2011-06-30 219 views
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]; 
} 

回答

0

看起來我正在使用來自另一個線程的託管對象(作爲我正在更新的對象的屬性添加)。