我很抱歉这个奇怪的标题,我是一个初学者,并且真的不知道怎么问这个,所以我会自己解释一下:
我正在学习核心数据,目前正在学习Apple示例代码。示例代码用于使用表格视图列出书籍。
在他们的AddViewController.h中,他们声明一个NSManagedObjectContext,如下所示:
#import "DetailViewController.h"
@protocol AddViewControllerDelegate;
@interface AddViewController : DetailViewController
@property (nonatomic, weak) id <AddViewControllerDelegate> delegate;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@end
@protocol AddViewControllerDelegate
- (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save;
@end
AddViewController.m
#import "AddViewController.h"
@implementation AddViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set up the undo manager and set editing state to YES.
[self setUpUndoManager];
self.editing = YES;
}
- (IBAction)cancel:(id)sender
{
[self.delegate addViewController:self didFinishWithSave:NO];
}
- (IBAction)save:(id)sender
{
[self.delegate addViewController:self didFinishWithSave:YES];
}
- (void)dealloc
{
[self cleanUpUndoManager];
}
@end
现在,在他们执行此代码的RootViewController(即主表视图)屏幕的委托方法中,他们执行此代码:
- (void)addViewController:(AddViewController *)controller didFinishWithSave:(BOOL)save {
if (save) {
NSError *error;
NSManagedObjectContext *addingManagedObjectContext = [controller managedObjectContext];
if (![addingManagedObjectContext 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();
}
if (![[self.fetchedResultsController 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();
}
}
// Dismiss the modal view to return to the main list
[self dismissViewControllerAnimated:YES completion:nil];
}
我不明白为什么他们将它保存到AddViewController类的managedObjectContext中? 我认为设置根视图控制器委托的背后的想法是这样我们可以在那里预先形成保存并传递上下文对象然后保存它......
请帮我搞定:/
答案 0 :(得分:0)
在Appdelegate.m
- (NSManagedObjectContext *)managedObjectContextInternal
{
if (__managedObjectContext != nil) {
return __managedObjectContext;
}
assert([NSThread isMainThread]); // must be creating context on main thread (though it might get read on a background thread from the library importers, but only to create the child contexts)
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
__managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
__managedObjectContext.undoManager = nil;
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
[self debugClearArtwork];
}
return __managedObjectContext;
}
如果要在创建或更新后保存数据
[APP_DEGATE saveContext]
项目中的任何地方只需要使用1个NSManagedObjectContext,并且应该调用mainThread。如果您不想在后台线程中创建NSManagedObjectContext,这是最佳实践。