我正在使用 coreData,我在文件夹和文件之间有一对多的关系。
我在整个应用程序中只使用了一个 MOC。我只是将它传递给不同的
viewControllers ,执行添加、编辑、删除和保存等操作。
我的 rootViewController 使用 NSfetchResultsController,我使用它创建文件夹,保存并显示在我的表上。
保存我这样做
NSError *error;
if (![self.managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
然后每当我选择一个文件夹时,我打开一个文件 ViewController,在打开时,我以这种方式将 MOC 传递给文件 VC
FileViewController *file = [[FileViewController alloc] initWithNibName:@"FileViewController" bundle:nil];
file.managedObjectContext = self.managedObjectContext;
file.controller = self.controller;
然后我在 FileVC 中创建一个文件并再次将它保存在 FileVC 中,这样
NSError *error;
if (![self.managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
通过这样做,我是使用两个 MOC 还是一个 MOC?
在我的 appdelegate.m 中,我也试过这个
self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
[self.managedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];
self.rootViewController.managedObjectContext = self.managedObjectContext;
有时当我在文件夹中添加文件时,我得到“NSManagedObject 的 NSMergeConflict”
请帮忙
问候兰吉特。