Search code examples
objective-ccore-datansmanagedobjectnsmanagedobjectcontext

NSManagedObjectContextObjectsDidChangeNotification NSDeletedObjectsKey Objects no longer has references to related objects


I want to respond to changes of certain NSManagedObjects that have been added, updated, or deleted.

I have an issue with the deleted objects, though: all relationships to other objects are now nil.

Is there a way to get this kind of notification before the object is affected this way?

Edit:

This is basically my delete code:

  [moc deleteObject:myObject];

  id saveBlock = ^{
      NSError *error = nil;
      BOOL     saved = NO;

     saved = [self save:&error];

    // error handling.
  };

  [moc performBlockAndWait:saveBlock];

Solution

  • If you are wanting to react to deletions then you should be listening for NSManagedObjectContextWillSaveNotification and watch for the NSDSeletedObjectsKey come through as part of the notification. That is the last chance before deletion to deal with them.