mergeChangesFromContextDidSaveNotification删除记录

时间:2011-06-30 18:29:28

标签: iphone core-data

我正在尝试在后台线程上更新CoreData中的记录。我根据文档为每个线程创建NSManagedObjectContext's。我也在注册保存数据的通知。但是,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];
}

1 个答案:

答案 0 :(得分:0)

看起来我正在使用另一个线程的托管对象(作为我正在更新的对象的属性添加)。