0
votes

i just wanted to make sure what am doing is right!

I have used Parent-Child NSManagedObjectContext pattern, where

i have one default private queue NSManagedObjectContext with type NSPrivateQueueConcurrencyType, and

i have one MainQueue NSManagedObjectContext with type NSMainQueueConcurrencyType , whose parent is the default private queue ,

and for each view controller i will create one private queue context with parent of main queue context,

it goes like this,

private context -> main context -> other context

so my question is, does this setup requires NSManagedObjectContextDidSaveNotification to propagate changes to parent context or it will be automatically bubbled up since all other context is child of parent and main context

because currently am using context save notification to merge changes and am getting following errors lot of times

Fatal Exception: NSInternalInconsistencyException This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.

2  CoreData                       0x2f2f44c9 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 3228
3  CoreData                       0x2f315db1 -[NSManagedObjectContext save:] + 824
4  App                        0x000a3279 -[CoreDataManager saveContext:withCompletionBlock:] (CoreDataManager.m:144)
5  App                        0x000a31f9 __46-[CoreDataManager contextDidSaveNotification:]_block_invoke (CoreDataManager.m:134)
6  CoreData                       0x2f3798f9 developerSubmittedBlockToNSManagedObjectContextPerform_privateasync + 68
1

1 Answers

0
votes

It seems like you are merging the notification to another child context. How I would typically do this is,

MainViewController

Main Context with persistent store coordinator Does not do any thing else than managing the child context.

ViewController 1

Has a child managed object context, whose parent is main context You would save this and push the changes to the main context like so,

[chilContext performBlock:^{
  [childContext save:&error];
  if(!error){
   [childContext.parentContext performBlock:^{
     [childContext.parentContext save:&error];
   }];
  }
}];

ViewController 2

Would you watching NSManagedObjectContextDidSaveNotification from parent object and if it finds the context being saved, it refreshes the objects by merging the changes from this notification.

- (void)parentContextSaved:(NSNotification*)note{
  if(![NSThread isMainThread]){
    [self performSelector:@selector(parentContextSaved:) onThread:[NSThread mainThread] withObject:note waitUntilDone:NO];
  }
  [childObjectContext performBlock:^{
    [childObjectContext mergeChangesFromContextDidSaveNotification:note];
   }];
}

I use this approach and this works well.