开发者

Core Data does not seem to take updated values

开发者 https://www.devze.com 2023-02-14 16:09 出处:网络
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 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 a开发者_运维百科gain, but is there anything else I should/can do to alert the mainthread that it should check the physical table again?


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号