NSManagedObjectContextObjectsDidChangeNotification for one object

Is it possible that NSManagedObjectContextObjectsDidChangeNotification will only get notified when a specific object has changed?

I want to update my view when my contact information or avatar changes, but with NSManagedObjectContextObjectsDidChangeNotification. I get a notification every time something changes in the database.

Can this be done with NSManagedObjectContextObjectsDidChangeNotification?

+3


source to share


1 answer


I don't believe this is only possible for a specific object. However, the notification provides information about which objects have changed. The notification contains a dictionary (userInfo) that contains 3 keys:

  • NSDeletedObjectsKey - an array of all remote objects
  • NSInsertedObjectsKey - an array of all objects that have been added / inserted
  • NSUpdatedObjectsKey - An array of all objects that have changed


You can loop over the contents of these arrays and determine if your particular object has changed. Below is an example:

- (void) handleObjectsChangedNotification:(NSNotification*) notification {
    // Iterate over all of the deleted objects
    for (NSManagedObject* object in notification.userInfo[NSDeletedObjectsKey]) {
    }

    // Iterate over all of the new objects
    for (NSManagedObject* object in notification.userInfo[NSInsertedObjectsKey]) {
    }

    // Iterate over all of the modified objects
    for (NSManagedObject* object in notification.userInfo[NSUpdatedObjectsKey]) {
    }
}

      

+4


source







All Articles