且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

NSFetchedResultsController数据未更新

更新时间:2022-03-12 07:54:56

NSManagedObjectContextDidSaveNotification ,我必须合并在后台线程中修改的上下文中的更改和主线程中的更改。我从这个问题中选择的答案得到了想法

Turns out I have to listen to NSManagedObjectContextDidSaveNotification and I have to merge the changes in the context modified in the background thread and the one in the main thread. I got the idea from the chosen answer in this question

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

//上下文中的更改必须合并

//Then the changes in the context has to be merged

- (void)contextDidSave:(NSNotification*)notification{
    NSLog(@"contextDidSave Notification fired.");
    SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
    [self.itemDatabase.managedObjectContext performSelectorOnMainThread:selector withObject:notification waitUntilDone:NO]; 
}