有时当我在RestKit MainQueueManagedObjectContext上调用saveToPersistent时,我收到以下错误,应用程序崩溃了。
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[UIBarButtonItem controllerWillChangeContent:]: unrecognized selector sent to instance 0x21e70300 with userInfo (null)
[UIBarButtonItem controllerWillChangeContent:]: unrecognized selector sent to instance 0x21e70300 with userInfo (null)
似乎存在某种线程问题。
我通过类似的方法保存上下文:
- (BOOL)saveMainContext
{
__block BOOL contextSaved = NO;
[[[[CoreDataManager sharedInstance] objectStore] mainQueueManagedObjectContext] performBlockAndWait:^{
NSError *error = nil;
if ([[[[CoreDataManager sharedInstance] objectStore] mainQueueManagedObjectContext] saveToPersistentStore:&error]) {
contextSaved = YES;
}
}];
return contextSaved;
}
使用EXC_BAD_ACCESS在saveToPersistent方法上崩溃。 我如何解决这个问题以及我的NSFetchresultscontroller在UIBarButtonItem上调用方法的原因是什么?
答案 0 :(得分:2)
只应从主线程中调用saveMainContext
方法,并且应该简单地实现为:
- (BOOL)saveMainContext
{
NSError *error = nil;
if ([[[[CoreDataManager sharedInstance] objectStore] mainQueueManagedObjectContext] saveToPersistentStore:&error]) {
contextSaved = YES;
} else {
NSLog(@"Save error: %@", error);
}
return contextSaved;
}
调用UIBarButtonItem
的实例表明您存在内存管理问题,因为观察上下文保存的内容在释放之前不会自行删除。检查所有观察员。