Search code examples
iphonecocoa-touchcore-datansfetchedresultscontroller

Core Data does not seem to take updated values


I am running an update thread which updates a table using coredata. One of the records is called last-update. But when after the update is finished I retrieve the last-update value in the main thread I get an outdated value.

I do:

[NSFetchedResultsController deleteCacheWithName:nil];

before query-ing the setting again, but is there anything else I should/can do to alert the mainthread that it should check the physical table again?


Solution

  • It sounds like you're not syncing the updates back into the main thread's NSManagedObjectController. Try adding a method like this:

    - (void)managedContextDidSave:(NSNotification *)n {
        if ([NSThread isMainThread]) {
            NSManagedObjectContext *context = /* Get context for main thread */;
            [context mergeChangesFromContextDidSaveNotification:n];
        } else {
            [self performSelectorOnMainThread:@selector(managedContextDidSave:) withObject:n waitUntilDone:YES];
        }
    }
    

    Then hook that up to the NSManagedObjectContextDidSaveNotification:

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