0

I'm using core data to persist my data model which is very simple, three entities like this: A<-->>B<-->>C. When something change in my model I need to perform some activity. To do so, I'm using listening to NSManagedObjectContextObjectsDidChangeNotification, using the following code in viewDidLoad:

[[NSNotificationCenter defaultCenter] 
               addObserver:self 
                  selector:@selector(updateReminder:)
                      name:NSManagedObjectContextObjectsDidChangeNotification 
               object:self.managedObjectContext];

In the method updateReminder I do what needed. The problem is that when the notification execute the changes aren't persisted. If new entity was created not get saved, if something is changed the change don`t get saved. Any ideas?

EDIT: The code of updateReminder is as follows:

- (void)updateReminder:(NSNotification *)notification
{
    A *entityAUpdated = [[notification userInfo] objectForKey:NSUpdatedObjectsKey];
    if (entityAUpdated) {
       ...Do something when entityA is updated.
    }
    A *entityADeleted = [[notification userInfo] objectForKey:NSDeletedObjectsKey];
    if (entityADeleted) {
       ...Do something when entityA is Deleted.
    }
    A *entityAInserted = [[notification userInfo] objectForKey:NSInsertedObjectsKey];
    if (entityAInserted) {
     ...Do something when entityA is Inserted
    }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Are you calling `save:` anywhere? It's not in the code you posted. – Tom Harrington Jul 01 '13 at 23:05
  • Yes, save: is called when the entity is created – Williams_Martinez Jul 01 '13 at 23:59
  • Changes are persisted only at `NSManagedObjectContextDidSaveNotification` (after the save). Here you are observing changes to your scratch-pad context (`NSManagedObjectContextObjectsDidChangeNotification`) – Dan Shelly Jul 02 '13 at 09:47
  • Yes, I'm observing and doing nothing. Why the managedObjectContext don't continue executing normally? – Williams_Martinez Jul 02 '13 at 13:30
  • When you say that data is not being saved, you mean it never appears? If you quit the app and re-launch, the new data isn't there? – Tom Harrington Jul 02 '13 at 15:58
  • Yes I mean that. And I just found the cause. My team mate changed something in the method that actually update the entity and do the save: She changed this line of code: A *entityToUpdate = (A *)self.entityArray[index]; for this line A *entityToUpdate = (self.entityArray)[index]; Where entityArray is a NSMutableArray of entities A This change cause the managedObjectContext to save nothing. Thank you very much for your time with this. – Williams_Martinez Jul 02 '13 at 16:40

0 Answers0