How to merge changes from one child managed entity to another through the parent MOC?

Hi (topical question below).

In iOS 5, there is an introduction to the CoreData contexts of the parent-child managed entity.

I have a standard NSFetchedResultsController and UITableVeiwController that work together to get the main list from the store. The managed object context processed by the final result is a child context with a parent context:

// AppDelegate.m - (NSManagedObjectContext *)managedObjectContext { if (__managedObjectContext != nil) { return __managedObjectContext; } __managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; // primary managed object context has NSPrivateQueueConcurrencyType [__managedObjectContext setParentContext:[self primaryObjectContext]]; return __managedObjectContext; } 

The table view controller is a modal view controller for adding a new record, but it uses a separate managed object context (this context is another child of the parent context). This context is stored in the delegate callback in the table view controller:

 - (void)addGame { // New child context [self setBuildManagedObectContext:[[NSManagedObjectContext alloc] init]]; [[self buildManagedObectContext] setParentContext:[[[self team] managedObjectContext] parentContext]]; Team *buildTeam = (Team *)[[self buildManagedObectContext] objectWithID:[[self team] objectID]]; Game *buildGame = [NSEntityDescription insertNewObjectForEntityForName:@"Game" inManagedObjectContext:[self buildManagedObectContext]]; [buildGame setTeam:buildTeam]; BuildViewController *buildVC = [[BuildViewController alloc] initWithGame:buildGame delegate:self]; UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:buildVC]; [[self navigationController] presentViewController:navCon animated:YES completion:nil]; } #pragma mark - Build view controller delegate - (void)buildViewController:(BuildViewController *)controller didFinishWithSave:(BOOL)save { if (save) { NSError *error = nil; if (![[self buildManagedObectContext] save:&error]) { NSLog(@"Error saving new game"); abort(); } } [self setBuildManagedObectContext:nil]; [[self navigationController] dismissViewControllerAnimated:YES completion:nil]; } 

Question:

As I understand it, with parent-child contexts, the parent context received a persistence notification from its child, and then notified all its children. Therefore, in my example, the assembly context must initiate the parent context to inform the recipient of the results in order to merge the changes.

This does not happen for me, records are created successfully, but the recipient of the results does not collect them. I know that this was the case that you needed to implement your merge from a save notification (as shown in CoreDataBooks ). But I thought that the context of the child-parent solved this problem. I tried to save the child and then the parent, but it did not seem to change the situation. Can someone explain this to me please? (Note: these are not contexts for single / background threads)

Thank you very much

+9
objective-c ios5 core-data
Mar 20 2018-12-12T00:
source share
1 answer

According to the WWDC 2011 presentation for β€œWhat's New in Core Data,” it says that you should keep the following:

 [child save:&error]; [parent performBlock:^{ [parent save:&parentError]; }]; 

From my understanding, this forces the parent to receive and merge the changes in the child context. It should be noted that the parent and all children must be created with the same type of concurrency.

- Edit the remote incorrect assumption that the parent is imposing changes on the children, this is not so.

+14
Mar 20 2018-12-18T00:
source share



All Articles