Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying which fields have changed before CoreData saves

//set up notifications

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(dataChanged:)
 name:NSManagedObjectContextDidSaveNotification
 object:context];    

//later

- (void)dataChanged:(NSNotification *)notification{
  NSDictionary *info = notification.userInfo;
  NSSet *insertedObjects = [info objectForKey:NSInsertedObjectsKey];
  NSSet *deletedObjects = [info objectForKey:NSDeletedObjectsKey];
  NSSet *updatedObjects = [info objectForKey:NSUpdatedObjectsKey];

Is there anyway to determine from the updatedObjects which fields were actually changed?

thanks, Michael

like image 424
Mike Avatar asked May 12 '11 10:05

Mike


People also ask

When managed object context saves its changes where does it push changes next?

When one of the managed object contexts is saved, its changes are pushed through the persistent store coordinator to the persistent store.

How do I get data from Core Data?

Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.

What is the purpose of NSManagedObjectContext?

A managed object context is an instance of NSManagedObjectContext . Its primary responsibility is to manage a collection of managed objects. These managed objects represent an internally consistent view of one or more persistent stores.

What is NSManagedObject in Core Data?

In some respects, an NSManagedObject acts like a dictionary—it's a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance.


1 Answers

The following should do the trick, but you will need to use NSManagedObjectContextWillSaveNotification and access your updated objects through the same NSManagedObjectContext used to save the objects.

for(NSManagedObject *obj in updatedObjects){

   NSDictionary *changes = [obj changedValues];
   // now process the changes as you need

}

See the discussion in the comments.

like image 174
Massimo Cafaro Avatar answered Sep 21 '22 13:09

Massimo Cafaro