1

I have three managed object context, namely :

masterManagedObjectContext(NSPrivateQueueConcurrencyType), 
backgroundMangedObjectContext(NSPrivateQueueConcurrencyType), 
uiManagedObjectContext(NSMainQueueConcurrencyType)

Master is the parent, and other two are its children. When I save any child context, I do save master context. But when backgroundMangedObjectContext saves in master, UIManagedObjectContext did not get the updated data in executeFetchRequest.

How do I achieve this? Please help. I am very much caught into this issue for past three days.

There were some posts suggest that, I must invalidate the previously fetched objects, when master gets saved. But I have no clue how to do it.

I tried

[UIManagedObjectContext reset]

But no luck.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

Try this,

You need to add observer in viewDidLoad or init method,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:)
name:NSManagedObjectContextDidSaveNotification object:nil];

and implement this method,

Wherever you call this method [context save:nil], it will call automatically and changes will update to masterContext.

- (void)contextDidSave:(NSNotification *)notification
{
    SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
    [[self masterManagedObjectContext] performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];

}
karthika
  • 4,085
  • 3
  • 21
  • 23
  • actually masterManagedObjectContext is getting the updated changes. But my uiManagedObjectContext is not. – Bharathi Jayakumar Oct 22 '13 at 12:25
  • in which mangedObjectContext you want to update, use this in this code.[yourManageObjectContext performSelectorOnMainThread:] – karthika Oct 22 '13 at 12:27
  • I also read that, after iOS5 we don't need to do this mergeChangesFromContextDidSaveNotification. When we use parent/child context, it will be automatically taken care of. Am I wrong? – Bharathi Jayakumar Oct 22 '13 at 12:28
  • if you use different contexts, you need to use this method. i'm also facing this issue in my project and i handled like this. – karthika Oct 22 '13 at 12:31