nicklockwood / AutoCoding

AutoCoding is a category on NSObject that provides automatic support for NSCoding and NSCopying to every object.
Other
1.07k stars 131 forks source link

NSKeyedUnarchiver fails to decode NSManagedObject subclass, previously encoded by NSKeyedArchiver #14

Open fatuhoku opened 10 years ago

fatuhoku commented 10 years ago

Specific Error

MESContinent is a NSManagedObject subclass. It has a String field called name.

[<MESContinent 0x9be07f0> setValue:forUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "name".
(
    0   CoreFoundation                      0x01bff5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x019828b6 objc_exception_throw + 44
    2   CoreFoundation                      0x01c8f6a1 -[NSException raise] + 17
    3   CoreData                            0x002514c4 -[NSManagedObject setValue:forUndefinedKey:] + 340
    4   Foundation                          0x015aff3b _NSSetUsingKeyValueSetter + 88
    5   CoreData                            0x001dbe28 -[NSManagedObject setValue:forKey:] + 120
    6   BDDReactiveCocoa                    0x0000bb47 -[NSObject(AutoCoding) setWithCoder:] + 919
    7   BDDReactiveCocoa                    0x0000bc4b -[NSObject(AutoCoding) initWithCoder:] + 59
    8   Foundation                          0x01595d6a _decodeObjectBinary + 3498
    9   Foundation                          0x01594e55 _decodeObject + 340
    10  Foundation                          0x01594cf9 -[NSKeyedUnarchiver decodeObjectForKey:] + 181
    11  Foundation                          0x016068cd +[NSKeyedUnarchiver unarchiveObjectWithData:] + 106

Reproduction

As a part of further investigation into #13, I wrote a failing test that doesn't use HRCoder. Please run it:

git clone https://github.com/fatuhoku/autocoding-demo
pod install
open BDDReactiveCocoa.xcworkspace

... and then run the BDDReactiveCocoaSpecs target on an iPad Simulator.

If you look in BDDReactiveCocoaSpecs/BDDObjectModelSpecs.m, you can see that I'm trying to just encode and decode an NSManagedObject subclass to and from NSData.

NSData *outgoingData = [NSKeyedArchiver archivedDataWithRootObject:europe];
// Pretend that we've stored it somewhere and we're now retrieving it
NSData *incomingData = outgoingData;
MESContinent *incomingEurope = [NSKeyedUnarchiver unarchiveObjectWithData:incomingData]; // Fails on this line.
mrtristan commented 10 years ago

potentially related: https://github.com/nicklockwood/FastCoding/issues/3

I'm sure there's a lot of similar code amongst these libraries and this looks awfully similar to what I just reported hopefully it's a small tweak that handles it on both sides