CoreData和mergeChangesFromContextDidSaveNotification

时间:2009-09-15 22:12:36

标签: iphone core-data

我正在创建一个AddingManagedObjectContext作为新实体的暂存区域,然后将新实体合并到我的主要ManagedObjectContext上的“Save”,类似于它在CoreDataBooks示例中的显示方式。

合并新实体后,如何快速引用它以用于显示详细视图?

您是否必须使获取结果控制器熄灭并再次执行获取(如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];  
}

1 个答案:

答案 0 :(得分:2)

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

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

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

希望这有帮助!