soffes / ssdatakit

Eliminate your Core Data boilerplate code
MIT License
453 stars 57 forks source link

[NSManagedObject shouldUnpackDictionary:]: unrecognized selector sent to instance #16

Closed webdevotion closed 11 years ago

webdevotion commented 11 years ago

I'm experiencing a crash in my app whenever I hit https://github.com/soffes/ssdatakit/blob/master/SSDataKit/SSRemoteManagedObject.m#L104

When I set a breakpoint on line 104 ( if ([object shouldUnpackDictionary:dictionary]) {) the application crashes ( log below ).

object is an instance of the class I would expect it to be. That class is a subclass of an APIClass, which inherits from SSRemoteManagedObject. I am surprised by the problem because when I log [self class] during execution, I see the class I'm expecting. But the crash mentions [NSManagedObject shouldUnpackDictionary:]. I understand my class is a sub-sub-sub-class of NSManagedObject, but it should try to send the message at least to SSRemoteManagedObject.

I would appreciate it if someone could give me a tip on how to debug this bug, which is probably in my implementation, not a bug in SSDatakit.

+ (id)objectWithDictionary:(NSDictionary *)dictionary context:(NSManagedObjectContext *)context {

    // Make sure we have a dictionary
    if (![dictionary isKindOfClass:[NSDictionary class]]) {
        return nil;
    }

    // Extract the remoteID from the dictionary
    NSNumber *remoteID = @([[dictionary objectForKey:@"id"] integerValue]);

    // Find object by remoteID
    SSRemoteManagedObject *object = [[self class] objectWithRemoteID:remoteID context:context];

    // Only unpack if necessary
    if ([object shouldUnpackDictionary:dictionary]) {
        [object unpackDictionary:dictionary];
    }

    // Return the new or updated object
    return object;
}

Output:

-[NSManagedObject shouldUnpackDictionary:]: unrecognized selector sent to instance 0x986dec0
2013-06-26 11:42:54.076 Plinx Client[18275:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObject shouldUnpackDictionary:]: unrecognized selector sent to instance 0x986dec0'
*** First throw call stack:
(0x25ae012 0x20a3e7e 0x26394bd 0x259dbbc 0x259d94e 0x189ffa 0x189e2a 0x3250 0x25a7e7c 0x25a7a16 0x25a7925 0x318c 0x8b598f 0x8b58ae 0x3078 0xcd9fa 0x8f728 0xccd4c 0xbd4dd 0xcccaf 0x926b3 0x5830 0x41839 0x232353f 0x2335014 0x23257d5 0x2554af5 0x2553f44 0x2553e1b 0x2ac17e3 0x2ac1668 0x15efffc 0x2b5d 0x2315)
libc++abi.dylib: terminate called throwing an exception
webdevotion commented 11 years ago

I finally found it. I started to inline some code, removed some dependencies on super classes etc. Still the same problem. But it enabled me to get closer to the real culprit. Namely Mr. Developer himself.

Turned out I did gave my Entity a custom class name, but made a typo. So the code was being executed on the default class of said Entity, namely NSManagedObject.