我正在爲新實體創建一個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];
}
那麼你的意思是,如果我救的「newBook」中的objectID時,它仍然是在addingManagedObjectContext(右後調用[addingManagedObjectContext節省:錯誤]」,我可以參考和使用它一旦合併完成嗎? –
是的,你可以在任何其他你所擁有的上下文中使用該objectID,並且假設你已經合併了來自添加上下文的變更,你將擁有最新的對象。 –