2009-09-15 71 views
3

我正在爲新實體創建一個addsManagedObjectContext作爲便箋區,然後將新實體合併到我的主'Manage'上的ManagedObjectContext中,類似於它在CoreDataBooks示例中的顯示方式。CoreData和mergeChangesFromContextDidSaveNotification

合併新實體後,如何快速參考它以顯示詳細視圖?

你必須使讀取結果控制器走出去,進行再次讀取(昂貴的,因爲在CoreDataBooks代碼說明)?我假設在'addingManagedObjectContext'中對象的初始id在合併後不會保持不變。

在食譜項目,沒有這個問題,因爲你正在創造和一個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]; 
} 

回答

2

看那些評論,它看起來像他們在談論獲取結果控制器。因此,在剛剛更改一個對象後,讓FRC執行新的提取操作會非常昂貴,因此,您可以將添加上下文與它合併,以通知它有任何更改。

一旦你進行保存和合並,你必須在添加上下文的引用的任何管理的對象不會有臨時的對象ID的更多,因爲他們在持久存儲存在。因此,您可以記住ID並在主應用程序上下文中使用[applicationContext objectWithID:newBookID],就可以很好地處理您要查找的對象。這將在應用程序的上下文中返回對象(包含所有更改)。

合併後,該對象可能存在於內存中,並且不需要訪問商店。然而,即使是這樣,因爲你只處理一個單獨的對象以在詳細視圖中顯示,它根本不是問題。去商店而不是上下文內存比較慢,但在你的應用程序中顯然必須發生很多次,除非你處理大量數據,否則不會引發問題!

希望這會有所幫助!

+0

那麼你的意思是,如果我救的「newBook」中的objectID時,它仍然是在addingManagedObjectContext(右後調用[addingManagedObjectContext節省:錯誤]」,我可以參考和使用它一旦合併完成嗎? –

+0

是的,你可以在任何其他你所擁有的上下文中使用該objectID,並且假設你已經合併了來自添加上下文的變更,你將擁有最新的對象。 –

相關問題