3

Here is the code:

- (void)willTurnIntoFault {
    NSManagedObject *relatedObject = self.relatedObject;
    if (relatedObject != nil && !relatedObject.isFault && !relatedObject.hasChanges) {
        [relatedObject.managedObjectContext refreshObject:relatedObject mergeChanges:NO];
    }

    [super willTurnIntoFault];
}

Here is the crash:

Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSObjectID_64_1 hasChanges]: unrecognized selector sent to instance 0x158601a0'

Last Exception Backtrace:
0   CoreFoundation                      0x307bfe8b __exceptionPreprocess + 131
1   libobjc.A.dylib                     0x3aab96c7 _objc_exception_throw + 39
2   CoreFoundation                      0x307c37b7 -[NSObject(NSObject) doesNotRecognizeSelector:] + 203
3   CoreFoundation                      0x307c20b7 ___forwarding___ + 707
4   CoreFoundation                      0x30710e98 __CF_forwarding_prep_0 + 24
5   Moke                                0x0017cd51 -[Status willTurnIntoFault] (Status.m:420)
6   CoreData                            0x305356eb -[NSFaultHandler turnObject:intoFaultWithContext:] + 67
7   CoreData                            0x305333bb -[NSManagedObject dealloc] + 75
8   CoreData                            0x30532b85 -[_PFManagedObjectReferenceQueue _processReferenceQueue:] + 1557
9   CoreData                            0x305393c7 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 3795
10  CoreData                            0x30532505 _performRunLoopAction + 317
11  CoreFoundation                      0x3078af71 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 21
12  CoreFoundation                      0x307888ff __CFRunLoopDoObservers + 287
13  CoreFoundation                      0x30788c4b __CFRunLoopRun + 739
14  CoreFoundation                      0x306f3541 _CFRunLoopRunSpecific + 525
15  CoreFoundation                      0x306f3323 _CFRunLoopRunInMode + 107
16  GraphicsServices                    0x3542a2eb _GSEventRunModal + 139
17  UIKit                               0x32faa1e5 _UIApplicationMain + 1137
18  Moke                                0x000f47e7 main (main.m:16)
19  libdyld.dylib                       0x3afb2ab7 start + 3

Here is the self object:

<Status: 0x16a705c0> (entity: Status; id: 0x16a31700 <x-coredata://66EAF889-7CD5-48C2-999E-5E15B84BF8FA/Status/p2235> ; data: {
    ID = 3622738251833351;
    relatedObject = "0x16a40340 <x-coredata://66EAF889-7CD5-48C2-999E-5E15B84BF8FA/Status/p2242>";
    text = "...";
    user = "0x16824200 <x-coredata://66EAF889-7CD5-48C2-999E-5E15B84BF8FA/User/p948>";
})

So it seems in this conditional !relatedObject.hasChanges relatedObject becomes _NSObjectID_64_1(which is a subclass of NSManagedObjectID). But how could it be?

PS: If you are curious why it doesn't crash at !relatedObject.isFault, it is because _NSObjectID_64_1 does implement ifFault by my test.

5
  • Are you accessing NSManagedObjects from multiple threads here?
    – bneely
    Sep 16, 2013 at 19:30
  • 1
    @bneely Never as I know.
    – an0
    Sep 16, 2013 at 19:33
  • Did you find the answer for your question? I have the same problem - my related NSManagedObject's occasionaly become NSManagedObjectID's, so my app crashes while sending unrecognized selectors.
    – kas-kad
    Jan 13, 2015 at 6:44
  • I have a guess that it was caused by a call to NSManagedObjectContext::reset. The reset invalidates all NSManagedObjects that belong to the context that reset is called upon. Continuing to use instances of NSManagedObject that have been invalidated can cause unexpected results. This is one of those unexpected results. Not sure about that though
    – kas-kad
    Jan 13, 2015 at 6:49
  • Another reason is that you are occasionally breaking a thread-confinement rule.
    – kas-kad
    Jan 13, 2015 at 7:22

2 Answers 2

0

It seems like you are transferring your managed objects between threads. Try to enhance your code this way:

//NSManagedObject *relatedObject = self.relatedObject;
NSManagedObject *relatedObject = [self.managedObjectContext existingObjectWithID:self.relatedObject.objectID error:nil];
-1

Can you call a message as a property ? As far as I know, hasChanges and isFault are messages.

I think it should be :-

if (relatedObject != nil && !([relatedObject isFault]) && !([relatedObject.hasChanges]))
1
  • someObject.message is translated by the compiler into [someObject message], even if message is not a property. Many people consider it bad style, but it works. - So this does probably not explain the above problem.
    – Martin R
    Sep 16, 2013 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.