Core data 是否立即启用文档NSManagedObjectContext的保存?

Core data 是否立即启用文档NSManagedObjectContext的保存?,core-data,core-data-migration,Core Data,Core Data Migration,从10.7上基于标准Xcode文档的应用程序w/CoreData模板开始,我遇到了一些令人沮丧的行为。我肯定我忽略了一些简单的事情 假设在我的NSPersistentDocument子类中,我有这样的东西,连接到窗口中的一个按钮: - (IBAction)doStuff:(id)sender { NSEntityDescription* ent = [[self.managedObjectModel entitiesByName] valueForKey: @"MyEnt

从10.7上基于标准Xcode文档的应用程序w/CoreData模板开始,我遇到了一些令人沮丧的行为。我肯定我忽略了一些简单的事情

假设在我的NSPersistentDocument子类中,我有这样的东西,连接到窗口中的一个按钮:

- (IBAction)doStuff:(id)sender
{        
    NSEntityDescription* ent = [[self.managedObjectModel entitiesByName] valueForKey: @"MyEntity"];
    NSManagedObject* foo = [[[NSManagedObject alloc] initWithEntity: ent insertIntoManagedObjectContext: self.managedObjectContext] autorelease];
    [self.managedObjectContext save: NULL];
}
如果我创建一个新文档并单击该按钮,我将得到以下错误:
此NSPersistentStoreCoordinator没有持久存储。它无法执行保存操作。
我明白了。我们还没有保存,没有持久存储。有道理

现在让我们假设我将其分为两个操作,连接到不同的按钮,如下所示:

- (IBAction)doStuff:(id)sender
{        
    NSEntityDescription* ent = [[self.managedObjectModel entitiesByName] valueForKey: @"MyEntity"];
    NSManagedObject* foo = [[[NSManagedObject alloc] initWithEntity: ent insertIntoManagedObjectContext: self.managedObjectContext] autorelease];
}

- (IBAction)doOtherStuff:(id)sender
{        
    [self.managedObjectContext save: NULL];
}
如果我创建一个新文档并按下第一个按钮,那么在按下该按钮(弄脏文档)后的某个不确定时间,autosave将出现并自动保存文档,这将在临时位置创建一个存储。如果我按下第二个按钮,就不会有投诉(因为现在有一家商店。)

我需要我的文档能够从一开始就进行managedObjectContext保存。我正在启动后台线程上的一些内容,我需要后台上下文的保存操作(和通知),以便将后台线程所做的更改合并到主线程的managedObjectContext中

我曾想过尝试强制执行自动保存,但自动保存过程似乎完全异步,因此我必须跳转以禁用任何可能导致managedObjectContext保存的UI交互,直到第一个自动保存操作完成

我还考虑创建一个内存存储,以弥补创建新文档和第一次自动保存之间的差距,但我不清楚如何将内存存储迁移到磁盘存储,并与第一次自动保存操作同步删除内存存储


有人对我如何处理这件事有什么想法吗?

如果你没有商店,你就无法保存。似乎您希望保存文档以便合并在后台线程上所做的更改;您可以手动合并这些更改。后台线程完成后,告诉主线程哪些对象已更新/插入,然后在主线程上进行相同的更改


如果更改几乎是任意的,因此复制起来很繁琐,那么您甚至可以在后台线程上构建自己的NSManagedObjectContextDidSaveNotification,然后在主线程上使用-[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:]将其合并。

所以我在这方面做了一会儿,包括尝试@Aderstedt的建议。这种方法不起作用,因为伪造通知似乎只是告诉接收上下文“嘿,检查持久存储,我已经更新了!”,而实际上我没有,因为没有。我最终找到了一种行之有效的方法。不幸的是,它只依赖于Lion的特性,所以我仍然在寻找一种不需要Lion的方法

背景 我想使用文档方法。虽然我在任何地方都没有找到明确的文档,但我找到了几个论坛帖子,并经历了一系列经验证据,证明您不能在属于NSPersistentDocument的上下文上调用
-[NSManagedObjectContext save::
。如问题中所述,如果在保存文档之前调用该函数,它将没有存储,因此保存将失败。即使在存储区存在之后,通过直接保存上下文(而不是通过document save API),您也可以有效地更改NSPersistentDocument背后的磁盘表示形式,并且您将获得显示以下内容的文档弹出表:

文件已被其他应用程序修改

简而言之,NSPersistentDocument希望控制关联的NSManagedObjectContext本身的保存操作

同样值得一提的是:这里的目标是确保UI使用的上下文不会触发(或至少最小)I/O以保持响应。我最终确定的模式是有3个上下文。NSPersistentDocument拥有的一个上下文,负责与文档一起执行文件I/O。第二个有效的只读上下文,用于将UI绑定到。(我意识到很多人都希望UI能够改变模型,所以这对他们来说可能不太令人兴奋,但这不是我的要求。)以及第三个用于后台线程的上下文,该线程从web服务异步加载数据,并希望将其推送到其他上下文中,这样它既可以保存在磁盘上,也可以显示在UI中,而不会潜在地阻塞网络I/O上的UI

狮子唯一的解决方案 Lion的CoreData实现中新的父/子NSManagedObjectContext功能非常适合于此。我将NSPersistentDocument的NSManagedObjectContext替换为并发类型为NSPrivateQueueConcurrencyType的新MOC。这将是“根”上下文。然后,我使用NSMainQueueConcurrencyType concurrency创建UI上下文,并将其作为根上下文的子上下文。最后,我将网络加载上下文设置为NSPrivateQueueConcurrencyType上下文,它是UI上下文的子级。其工作方式是我们在后台启动网络加载操作,它会更新网络上下文。完成后,它会保存上下文。对于父/子关系,保存子上下文会将更改向上推送到父上下文(UI上下文)中,但不会将父上下文保存到存储中。在我的例子中,我还从网络上下文中侦听NSManagedObjectContextDidSaveNotification通知,然后告诉它的父级也保存(这将把UI上下文中的更改推送到根/磁盘上下文中,但不会将其保存到磁盘)

在这一连串事件的末尾,所有的c
@interface NSManagedObjectContext (MergeChangesFromObjectsDidChangeNotification)
- (void)mergeChangesFromObjectsDidChangeNotification: (NSNotification*)notification;
@end

@implementation NSManagedObjectContext (MergeChangesFromObjectsDidChangeNotification)

- (void)mergeChangesFromObjectsDidChangeNotification: (NSNotification*)notification
{
    if (![NSManagedObjectContextObjectsDidChangeNotification isEqual: notification.name])
        return;

    if (notification.object == self)
        return;

    NSManagedObjectContext* sourceContext = (NSManagedObjectContext*)notification.object;

    NSAssert(self.persistentStoreCoordinator == sourceContext.persistentStoreCoordinator, @"Can't merge changes between MOCs with different persistent store coordinators.");

    [sourceContext lock];

    // Create object in the local context to correspond to inserted objects...
    NSMutableDictionary* foreignOIDsToLocalOIDs = [NSMutableDictionary dictionary];
    for (NSManagedObject* foreignMO in [[notification userInfo] objectForKey: NSInsertedObjectsKey])
    {
        NSManagedObjectID* foreignOID = foreignMO.objectID;
        NSManagedObject* localMO = [[[NSManagedObject alloc] initWithEntity: foreignMO.entity insertIntoManagedObjectContext: self] autorelease];
        [foreignOIDsToLocalOIDs setObject: localMO.objectID forKey: foreignOID];
    }

    // Bring over all the attributes and relationships...
    NSMutableSet* insertedOrUpdated = [NSMutableSet set];
    [insertedOrUpdated unionSet: [[notification userInfo] objectForKey: NSInsertedObjectsKey]];
    [insertedOrUpdated unionSet: [[notification userInfo] objectForKey: NSUpdatedObjectsKey]];

    for (NSManagedObject* foreignMO in insertedOrUpdated)
    {
        NSManagedObjectID* foreignOID = foreignMO.objectID;
        NSManagedObjectID* localOID = [foreignOIDsToLocalOIDs objectForKey: foreignOID];
        localOID = localOID ? localOID : foreignOID;
        NSManagedObject* localMO = [self objectWithID: localOID];

        // Do the attributes.
        [localMO setValuesForKeysWithDictionary: [foreignMO dictionaryWithValuesForKeys: [[foreignMO.entity attributesByName] allKeys]]];

        // Do the relationships.
        NSDictionary* rByName = foreignMO.entity.relationshipsByName;
        for (NSString* key in [rByName allKeys])
        {
            NSRelationshipDescription* desc = [rByName objectForKey: key];
            if (!desc.isToMany)
            {
                NSManagedObject* relatedForeignMO = [foreignMO valueForKey: key];
                NSManagedObjectID* relatedForeignOID = relatedForeignMO.objectID;
                NSManagedObjectID* relatedLocalOID = [foreignOIDsToLocalOIDs objectForKey: relatedForeignOID];
                relatedLocalOID = relatedLocalOID ? relatedLocalOID : relatedForeignOID;
                NSManagedObject* localRelatedMO = [self objectWithID: relatedLocalOID];
                [localMO setValue: localRelatedMO forKey: key];
            }
            else
            {
                id collection = [foreignMO valueForKey: key];
                id newCollection = [NSMutableSet set];
                if ([collection isKindOfClass: [NSOrderedSet class]])
                {
                    newCollection = [NSOrderedSet orderedSet];
                }

                for (NSManagedObject* relatedForeignMO in collection)
                {
                    NSManagedObjectID* relatedForeignOID = relatedForeignMO.objectID;
                    NSManagedObjectID* relatedLocalOID = [foreignOIDsToLocalOIDs objectForKey: relatedForeignOID];
                    relatedLocalOID = relatedLocalOID ? relatedLocalOID : relatedForeignOID;
                    NSManagedObject* localRelatedMO = [self objectWithID: relatedLocalOID];
                    [newCollection addObject: localRelatedMO];
                }
                [localMO setValue: newCollection forKey: key];
            }
        }
    }

    // And delete any objects which pre-existed in my context.
    for (NSManagedObject* foreignMO in [[notification userInfo] objectForKey: NSDeletedObjectsKey])
    {
        NSManagedObjectID* foreignOID = foreignMO.objectID;
        NSManagedObject* localMO = [self existingObjectWithID: foreignOID error: NULL];
        if (localMO)
        {
            [self deleteObject: localMO];
        }
    }

    [sourceContext unlock];
}

@end
- (NSManagedObjectContext *)managedObjectContext {
    if (!_context) {
        NSManagedObjectContext *_default = [super managedObjectContext];
        _context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        _context.persistentStoreCoordinator = _default.persistentStoreCoordinator;
    }

    return _context;
}