为啥委托方法需要将自定义类托管对象上下文的内容保存在委托类托管对象上下文中?
Posted
技术标签:
【中文标题】为啥委托方法需要将自定义类托管对象上下文的内容保存在委托类托管对象上下文中?【英文标题】:Why does a delegate method need to save content of a custom class managed object context and in the delegate class managed object context?为什么委托方法需要将自定义类托管对象上下文的内容保存在委托类托管对象上下文中? 【发布时间】:2014-04-21 06:16:18 【问题描述】:对不起这个奇怪的标题,我是初学者,真的不知道如何问这个问题,所以我会解释一下自己:
我正在学习 Core Data,目前正在学习 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 中......? 我认为将根视图控制器设置为委托背后的漏洞想法是,我们可以在那里执行保存并传递上下文对象,然后保存它......
请帮我搞定:/
【问题讨论】:
AddViewController 的 NSManagedObjectContext 变量来自哪里?它来自 AppDelegate,对吧? 【参考方案1】:在 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,这是最佳实践。
【讨论】:
所以因为我没有在我的 AddViewController.m 中声明 - (NSManagedObjectContext *) managedObjectContext 我必须从主线程调用它..? @nmh 不明白。在 AddViewController.m 中,如何获取 NSManagedObjectContext?它是从 AppDelegate 获得的吗?如果是,我认为一切正常。以上是关于为啥委托方法需要将自定义类托管对象上下文的内容保存在委托类托管对象上下文中?的主要内容,如果未能解决你的问题,请参考以下文章
如何通过应用程序委托使用托管对象上下文实例化还原的视图控制器?