My application uses the NSFetchedResultsController associated with the Core Data repository, and it still worked fine, but now I'm trying to make an asynchronous update code, and I'm having problems. I subclassed the NSOperation class to perform my updates and successfully add this new object to NSOperationQueue. The update code is executing as I expect, and I checked this through the debug logs and checked the SQLite repository after it started.
The problem is that after completing my background operation, new (or updated) items do not appear in my UITableView. Based on my limited understanding, I believe that I need to notify the main managedObjectContext file that changes have occurred so that they can be merged. My notification is triggered, the nut does not contain new elements in the table view. If I stopped the application and restarted it, the objects will appear in the table view, which will lead me to the fact that they will be successfully inserted into the master data store, but will not be combined into a managed object The object used in the main thread.
I have included a sample of my init, main operations, and notification methods. Am I missing something important or maybe this is not right? Any help would be greatly appreciated.
- (id)initWithDelegate:(AppDelegate *)theDelegate { if (!(self = [super init])) return nil; delegate = theDelegate; return self; } - (void)main { [self setUpdateContext:[self managedObjectContext]]; NSManagedObjectContext *mainMOC = [self newContextToMainStore]; NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:updateContext]; [self setMainContext:mainMOC]; // Create/update objects with mainContext. NSError *error = nil; if (![[self mainContext] save:&error]) { DLog(@"Error saving event to CoreData store"); } DLog(@"Core Data context saved"); } - (void)contextDidSave:(NSNotification*)notification { DLog(@"Notification fired."); SEL selector = @selector(mergeChangesFromContextDidSaveNotification:); [[delegate managedObjectContext] performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES]; }
During debugging, I examined the notification object, which is sent to contextDidSave: and it seems to contain all the elements that were added (excerpt below). This continues to make me think that inserts / updates are happening correctly, but somehow the merge does not start.
NSConcreteNotification 0x6b7b0b0 {name = NSManagingContextDidSaveChangesNotification; object = <NSManagedObjectContext: 0x5e8ab30>; userInfo = { inserted = "{(\n <GCTeam: 0x6b77290> (entity: GCTeam; id: 0xdc5ea10 <x-coredata://F4091BAE-4B47-4F3A-A008-B6A35D7AB196/GCTeam/p1> ; data: {\n changed =
source share