How do we prevent “CoreData could not fulfill a fault”?

倖福魔咒の 提交于 2019-12-03 14:26:22

You can register for change notifications in Core Data. This will allow you to update your managed objects when they change. See the Core Data Docs for more info. You're going to be interested in 2 methods to register and respond to changes:

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

The mergeChanges selector (your method) will call the following method to synchronize any changes from other threads. It will look something like this:

- (void)mergeChanges:(NSNotification *)notification{
  AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *context = [appDelegate managedObjectContext];

  // Merge changes into the default context on the main thread
  [context performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                            withObject:notification
                         waitUntilDone:YES];  
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!