0
votes

I currently have 2 contexts in my app. My app uses multiple tabs so one tab could be displaying data whilst another tab might be in data entry mode.

There is one main context I use to read most of the data for display. When I insert data I am using a second temporary context as I don't want the operation of other tabs to save possibly incomplete objects added to the context.

I have been reading apples concurrency guide: http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/Articles/cdConcurrency.html#//apple_ref/doc/uid/TP40003385-SW1

It indicates that you should use notifications to propagate changes between contexts.

Both of my contexts use the same propagation store object. My question is if a change is made to context A in tab A, when a fetch request is reissued in tab B using context B will the changes simply show up in tab B?

This is what I am seeing at the moment. I assume core data caching is done at the propogation store level? If thats correct then synchronizing contexts is only required where you don't intend to re run a fetch query so you can selectively update only the NSManagedObjects that have changed?

This is how I am getting my new context:

   NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    NSManagedObjectContext *newContext;
    if (coordinator != nil) {
        newContext = [[NSManagedObjectContext alloc] init];
        [newContext setPersistentStoreCoordinator:coordinator];
    }
2

2 Answers

1
votes

If you want the data that you modify in Tab B to show up in Tab A (and vise versa) when you are using two different NSManagedObjectContexts you need to add an observer to the notification

NSManagedObjectContextDidSaveNotification

in the controller code for Tab A (and B if changes are being made in A)

as in

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

Then at the declared selector

-(void)mergeChanges:(NSNotification *)anotif
{
    [self.managedObjectContext mergeChangesFromContextDidSaveNotification:anotif];
    [self updateTheUI];
}
0
votes

"Changes you make to a managed object in one context are not propagated to a corresponding managed object in a different context unless you either refetch or re-fault the object."