How to sync two NSManagedObjectContext


I am working on an ipad application using coredata. It downloads information about a database that is on the Internet and writes it to coredata. The application is based on a split view. My problem was to do the loading and recording of data in the background.
Here's how I did it:
- I created NSOperation, which downloads and writes data.
- This NSOperation uses a different NSManagedObjectContext than the appDelegate context returned by this function, which is in appDelegate:

(NSManagedObjectContext*)newContextToMainStore {
     NSPersistentStoreCoordinator *coord = nil;
     coord = [self persistentStoreCoordinator];
     NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
     [moc setPersistentStoreCoordinator:coord]; 
     return [moc autorelease];
}

- I had an observer in NSOperation that will call this function in appDelegate while maintaining the context in order to change the delegate context:

- (void)mergeChangesFromContextSaveNotification:(NSNotification*)notification {
     [[self managedObjectContext]mergeChangesFromContextDidSaveNotification:notification];
}

, , rootViewController ( UITableViewController), NSManagedObjectContext, appDelegate NSFetchedResultsController, t , .
:
? ?

+3
1

, , . , NSFetchedResultControllerDelegate , .

+1

Source: https://habr.com/ru/post/1754142/


All Articles