Iphone CoreData和mergeChangesFromContextDidSaveNotification

Iphone CoreData和mergeChangesFromContextDidSaveNotification,iphone,core-data,Iphone,Core Data,我正在创建一个addingManagedObjectContext作为新实体的暂存区,然后在“保存”中将新实体合并到我的主ManagedObjectContext中,类似于CoreDataBooks示例中所示 合并新实体后,如何快速引用该实体以用于显示局部视图 您是否必须让fetch result controller退出并再次执行fetch(正如CoreDataBooks代码中指出的那样昂贵)?我假设对象在“addingManagedObjectContext”中的初始id在合并后不会保持不变

我正在创建一个addingManagedObjectContext作为新实体的暂存区,然后在“保存”中将新实体合并到我的主ManagedObjectContext中,类似于CoreDataBooks示例中所示

合并新实体后,如何快速引用该实体以用于显示局部视图

您是否必须让fetch result controller退出并再次执行fetch(正如CoreDataBooks代码中指出的那样昂贵)?我假设对象在“addingManagedObjectContext”中的初始id在合并后不会保持不变

在Recipes项目中,没有这个问题,因为您正在一个ManagedObjectContext中创建和使用新实体。因此,您可以引用新创建的项以显示其详细视图

从CoreDataBooks:

/**
 Add controller's delegate method; informs the delegate that the add operation has completed, and indicates 
 whether the user saved the new book.
 */
- (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save {

    if (save) {
        /*
         The new book is associated with the add controller's managed object context.
         This is good because it means that any edits that are made don't affect the 
         application's main managed object context -- it's a way of keeping disjoint edits 
         in a separate scratchpad -- but it does make it more difficult to get the new book 
         registered with the fetched results controller.

         First, you have to save the new book.  This means it will be added to the persistent 
         store.  Then you can retrieve a corresponding managed object into the application 
         delegate's context.  Normally you might do this using a fetch or using objectWithID: -- for example

         NSManagedObjectID *newBookID = [controller.book objectID];
         NSManagedObject *newBook = [applicationContext objectWithID:newBookID];

         These techniques, though, won't update the fetch results controller, which 
         only observes change notifications in its context.

         You don't want to tell the fetch result controller to perform its fetch again 
         because this is an expensive operation.

         You can, though, update the main context using mergeChangesFromContextDidSaveNotification: which 
         will emit change notifications that the fetch results controller will observe.
         To do this:
         1  Register as an observer of the add controller's change notifications
         2  Perform the save
         3  In the notification method (addControllerContextDidSave:), merge the changes
         4  Unregister as an observer
         */
        NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
        [dnc addObserver:self selector:@selector(addControllerContextDidSave:) name:NSManagedObjectContextDidSaveNotification object:addingManagedObjectContext];

        NSError *error;
        if (![addingManagedObjectContext save:&error]) {
            // Update to handle the error appropriately.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);  // Fail
        }
        [dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:addingManagedObjectContext];
    }
    // Release the adding managed object context.
    self.addingManagedObjectContext = nil;

    // Dismiss the modal view to return to the main list
    [self dismissModalViewControllerAnimated:YES];
}


/**
 Notification from the add controller's context's save operation. This is used to update the 
 fetched results controller's managed object context with the new book instead of performing 
 a fetch (which would be a much more computationally expensive operation).
 */
- (void)addControllerContextDidSave:(NSNotification*)saveNotification {

    NSLog(@"addControllerContextDidSave");
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    // Merging changes causes the fetched results controller to update its results
    [context mergeChangesFromContextDidSaveNotification:saveNotification];  
}

看看这些评论,看起来他们在谈论获取的结果控制器。因此,在您刚刚更改了一个对象之后,让FRC执行新的获取将是相当昂贵的,因此您可以将添加上下文与它合并,以通知它任何更改

执行保存和合并后,在添加上下文中引用的任何托管对象将不再具有临时对象ID,因为它们存在于持久存储中。因此,您可以记住ID,并在主应用程序上下文中使用[applicationContext objectWithID:NewBookKid]来获得所查找对象的句柄。这将返回应用程序上下文中的对象(及其所有更改)

合并后,对象可能存在于内存中,不需要访问存储。但是,即使是这样,因为您只处理要在详细视图中显示的单个对象,所以这根本不是问题。去商店而不是在上下文内存中是比较慢的,但是很明显,在你的应用程序中会发生很多次,除非你处理大量数据,否则不太可能引起问题


希望这有帮助

因此,您的意思是,如果我保存'newBook'的objectID,而它仍然在addingManagedObjectContext中(就在调用[addingManagedObjectContext save:&error]之后),我可以引用它,并在合并完成后使用它?是的,您可以在任何其他上下文中使用该objectID,并且假设您已合并添加上下文中的更改,您将拥有最新的对象。