0

I have an implementation of Core Data where I need to listen to the NSManagedObjectContextDidSaveNotification notification, but it simply doesn't get dispatched.

[[NSNotificationCenter defaultCenter] 
           addObserver:self 
              selector:@selector(method:) 
                  name:NSManagedObjectContextDidSaveNotification 
                object:nil];

In my implementation if I switch to the NSManagedObjectContextWillSaveNotification everything works as expected, ie, my method: selector gets invoked.

To trigger the notification, I'm simply doing this:

NSManagedObject *object = [NSEntityDescription 
               insertNewObjectForEntityForName:@"Object" 
                        inManagedObjectContext:_moc];
if (![_moc save:nil]) { 
  NSLog(@"saving successful, expecting notification dispatch");
}

But nothing... Please halp

EDIT: If I manually post the notification NSManagedObjectContextDidSaveNotification inside the save if statement, it gets dispatched and my method: gets called, but my NSManagedObjectContext just won't do it automatically on save as it should...

JackyJohnson
  • 3,106
  • 3
  • 28
  • 35
  • Any help? http://stackoverflow.com/questions/19219340/nsmanagedobjectcontextdidsavenotification-not-triggered-in-ios-7 – Sunkas Mar 16 '15 at 09:57
  • no, my problem is that the notification doesn't even get dispatched. I've tried moving everything to the same thread, same thing. what's really odd to me is the willSaveNotification gets dispatched normally but not the didSave- – JackyJohnson Mar 16 '15 at 09:58
  • Does your `NSLog` print the "saving successful" message? You didn't say, and it might be important. – Tom Harrington Mar 16 '15 at 16:43
  • i literally just found the problem before i read your post. i'm retarded... – JackyJohnson Mar 16 '15 at 16:45
  • @Tom Harrington just wanna say thank you for the help! – JackyJohnson Mar 16 '15 at 16:47

1 Answers1

0

The problem was that the save was actually returning an error. So obviously it didn't save.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
JackyJohnson
  • 3,106
  • 3
  • 28
  • 35