我想通過調用mergeChangesFromContextDidSaveNotification與CoreData NSFetchedResultsController通過調用addingManagedObjectContext到我的主managedObjectContext中的更改。像這樣:NSManagedObjectController沒有在從addsManagedObjectContext更改合併
- (void)addGoalViewController:(AddGoalViewController *)controller didFinishWithSave:(BOOL)save {
if (save) {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self
selector:@selector(addControllerContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:addingManagedObjectContext];
NSLog(@"Before save - addingManagedObjectContext - Number of registered objects: %d", [[addingManagedObjectContext registeredObjects] count]);
NSLog(@"Before save - addingManagedObjectContext - Number of inserted objects: %d", [[addingManagedObjectContext insertedObjects] count]);
NSLog(@"Before save - addingManagedObjectContext - Number of updated objects: %d", [[addingManagedObjectContext updatedObjects] count]);
NSLog(@"Before save - addingManagedObjectContext - Number of deleted objects: %d", [[addingManagedObjectContext deletedObjects] count]);
// Save the new object to the adding managed object context
NSError *error = nil;
if ([addingManagedObjectContext hasChanges] && [addingManagedObjectContext save:&error] == NO) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
NSLog(@"After save - addingManagedObjectContext - Number of registered objects: %d", [[addingManagedObjectContext registeredObjects] count]);
NSLog(@"After save - addingManagedObjectContext - Number of inserted objects: %d", [[addingManagedObjectContext insertedObjects] count]);
NSLog(@"After save - addingManagedObjectContext - Number of updated objects: %d", [[addingManagedObjectContext updatedObjects] count]);
NSLog(@"After save - addingManagedObjectContext - Number of deleted objects: %d", [[addingManagedObjectContext deletedObjects] count]);
// Remove the adding context observer
[dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:addingManagedObjectContext];
}
// Reset the adding context back to its default state
[addingManagedObjectContext reset];
// Release the adding managed object context.
self.addingManagedObjectContext = nil;
// Dismiss the add goal view to return to the main list
[self dismissModalViewControllerAnimated:YES];
}
我正在關注CoreDataBooks示例當我嘗試合併更改但儘管它不起作用。這裏是我的合併代碼:
- (void)addControllerContextDidSave:(NSNotification *)saveNotification {
NSManagedObjectContext* context = [fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
NSLog(@"Before merge - managedObjectContext - Number of registered objects: %d", [[context registeredObjects] count]);
NSLog(@"Before merge - managedObjectContext - Number of inserted objects: %d", [[context insertedObjects] count]);
NSLog(@"Before merge - managedObjectContext - Number of updated objects: %d", [[context updatedObjects] count]);
NSLog(@"Before merge - managedObjectContext - Number of deleted objects: %d", [[context deletedObjects] count]);
[context mergeChangesFromContextDidSaveNotification:saveNotification];
NSLog(@"After merge - managedObjectContext - Number of registered objects: %d", [[context registeredObjects] count]);
NSLog(@"After merge - managedObjectContext - Number of inserted objects: %d", [[context insertedObjects] count]);
NSLog(@"After merge - managedObjectContext - Number of updated objects: %d", [[context updatedObjects] count]);
NSLog(@"After merge - managedObjectContext - Number of deleted objects: %d", [[context deletedObjects] count]);
}
不幸的是,它似乎並沒有被添加對象到我的主要管理對象的情況下,將其添加到我的addingManagedObjectContext但合併後,應該將它們合併到主管理對象上下文。但是,我的主管理對象上下文中的註冊對象數保持爲0.保存後,請參閱addingManagedObjectContext中的1個註冊對象。這應該是我的主要managedObjectContext,因爲我合併它,但它不是。它堅持在我的addsManagedObjectContext中。
我已經看過了蘋果類引用文件,從我讀它好像
Before save - addingManagedObjectContext - Number of registered objects: 1
Before save - addingManagedObjectContext - Number of inserted objects: 1
Before save - addingManagedObjectContext - Number of updated objects: 1
Before save - addingManagedObjectContext - Number of deleted objects: 0
Before merge - managedObjectContext - Number of registered objects: 0
Before merge - managedObjectContext - Number of inserted objects: 0
Before merge - managedObjectContext - Number of updated objects: 0
Before merge - managedObjectContext - Number of deleted objects: 0
After merge - managedObjectContext - Number of registered objects: 0
After merge - managedObjectContext - Number of inserted objects: 0
After merge - managedObjectContext - Number of updated objects: 0
After merge - managedObjectContext - Number of deleted objects: 0
After save - addingManagedObjectContext - Number of registered objects: 1
After save - addingManagedObjectContext - Number of inserted objects: 0
After save - addingManagedObjectContext - Number of updated objects: 0
After save - addingManagedObjectContext - Number of deleted objects: 0
正如你從這個輸出是該對象添加到addingManagedObjectContext看到,但它不合並該對象轉換爲主managedObjectContext。相反,該對象保留在addingManagedObjectContext中。從Apple Class Reference看來,這似乎不應該發生。相反,它應該將對象合併到managedObjectContext中。並且似乎沒有任何方法可以檢查mergeChangesFromContextDidSaveNotification錯誤。
NSFetchedResultsController不更新,並自動爲我合併數據。即使這樣做,我明確告訴它保存未保存的更改,但仍然無效。我相信這是我的錯,但我認爲這是幾乎不可能寫出一個好的Objective-C問題,因爲語言太冗長了。 –