CoreData和mergeChangesFromContextDidSaveNotification [英] CoreData and mergeChangesFromContextDidSaveNotification

查看:169
本文介绍了CoreData和mergeChangesFromContextDidSaveNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个添加ManagedObjectContext作为一个新的实体的暂时区域,然后合并新的实体到我的主要ManagedObjectContext'保存',类似于它在CoreDataBooks示例中显示。

I'm creating a addingManagedObjectContext as a scratch pad area for new entities and then merge the new entity into my main ManagedObjectContext on 'Save', similar to how it's shown in CoreDataBooks Example.

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

After you merge your new entity, how do you get a quick reference to it for use in displaying a detail view?

您必须使撷取结果控制器外出并再次执行撷取(如CoreDataBooks程式码中所述,价格昂贵)?我假定在addingManagedObjectContext中的对象的初始ID在合并后将不会保持不变。

Do you have to make the fetch result controller go out and perform a fetch again (expensive as noted in the CoreDataBooks code)? I'm presuming that the initial id of the object when in the 'addingManagedObjectContext' will not remain the same after it has been merged.

在Recipes项目中,因为你正在一个ManagedObjectContext中创建和使用新的实体。因此,您有一个对新创建的项目的引用以显示它的详细视图。

In the Recipes project, there isn't this concern because you're creating and working with the new entity in one ManagedObjectContext. Therefore, you have a reference to the newly created item to show a detail view of it.

从CoreDataBooks:

From 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执行新的提取将是相当昂贵的,因此您可以合并您的添加上下文,以通知它任何更改。

Looking at those comments, it looks like they're talking about fetched results controllers. So it would be rather expensive to get the FRC to perform a new fetch after you've just changed one object, therefore you can merge your adding context with it to notify it of any changes.

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

Once you have performed the save and merge, any managed objects that you have a reference to in the adding context won't have temporary object ID's any more as they exist in the persistent store. Therefore you can remember the ID's and use [applicationContext objectWithID:newBookID] in the main application context just fine to get a handle on the object you're looking for. That will return the object (with all it's changes) in the application's context.

合并后,对象很可能存在于内存中,不需要访问存储。然而,即使是,因为你只处理一个单一的对象在详细视图中显示它根本不是一个问题。去商店而不是上下文内存是更慢,但显然必须在你的应用程序发生一大堆的时间,它不可能引起问题,除非你处理大量的数据!

After the merge, it's probable that the object exists in memory and no trip to the store is required. However, even if it is, as you're only dealing with a single object to display in a detail view it's not a problem at all. Going to the store instead of in-context-memory is slower but obviously has to happen a whole bunch of times during your app and it's unlikely to cause issues unless you're dealing with a LOT of data!

希望这有助于!

这篇关于CoreData和mergeChangesFromContextDidSaveNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆