0
我想在後臺線程上更新CoreData中的記錄。我爲每個文檔創建了每個線程NSManagedObjectContext's
。我也正在註冊數據保存時間的通知。但是,mergeChangesFromContextDidSaveNotification
正在刪除條目而不是更新它們。mergeChangesFromContextDidSaveNotification刪除記錄
//Notification callback
- (void)_managedObjectContextDidSave:(NSNotification *)notification
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
});
}
//per thread managed object context
- (NSManagedObjectContext *)managedObjectContext
{
if ([[[NSThread currentThread] threadDictionary] objectForKey:GVControllerManagedObjectContextKey] == nil) {
NSPersistentStoreCoordinator *coordinator = self._persistentStoreCoordinator;
if (coordinator != nil) {
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
if (![NSThread isMainThread]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_managedObjectContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:managedObjectContext];
}
[[[NSThread currentThread] threadDictionary] setObject:managedObjectContext forKey:GVControllerManagedObjectContextKey];
[managedObjectContext release];
}
}
return [[[NSThread currentThread] threadDictionary] objectForKey:GVControllerManagedObjectContextKey];
}