I am using a background thread to do a long running task including pulling data from service and insert records into the database via CoreData. As suggested by many answers here I am observing the NSManagedObjectContextDidSaveNotification notification. I am using these 2 methods. In mergeChanges method I see my insertions in the notification object but after both methods complete my main MOC shows only changes prior to the background thread. Why is it not reflecting new changes from the background thread. What am i missing?
- (void) registerContextDidSaveNotificationForManagedObjectContext: (NSManagedObjectContext*) moc {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:moc];
}
- (void)mergeChanges:(NSNotification *)notification {
//AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = self.managedObjectContext;
// Merge changes into the main context on the main thread
[mainContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}