1
我环顾四周,看看人们如何使用核心数据添加vs编辑。我遇到了WWDC 2012 - 214视频以及这篇文章:http://www.cocoanetics.com/2012/07/multi-context-coredata/,这篇文章讨论了如何在父级使用子级上下文。我的问题是,如果我需要在一个ViewController上创建一个临时对象,然后将这个临时对象传递给一对viewController,直到我决定是否要保存或不保存,该怎么办?我是否需要通过viewControllers将tempContext传递给我?或者我只需要将NSManagedObject传递给其他viewControllers,并且tempContext是方法的局部变量并不重要。例如:在视图控制器中保持儿童环境
视图控制器1的作用:
@property (nonatomic, strong) Route *route; // NSManagedObject subclass
@property (nonatomic, strong) NSManagedObjectContext *mainMoc;
- (void)calculateRoute {
NSMangedObjectContext *temporaryContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
temporaryContext.parentContext = self.mainMOC;
// calculate the route to possibly save at least one viewController deep
self.route = route;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIViewController *destination = [segue destinationViewController];
if ([segue.identifier isEqualToString:@"ShowDistanceViewController"]) {
if ([destination respondsToSelector:@selector(setManagedObjectContext:)]) {
[destination setValue:self.managedObjectContext forKey:@"managedObjectContext"];
}
if ([destination respondsToSelector:@selector(setRoute:)]) {
[destination setValue:self.route forKey:@"route"];
}
}
现在在destinationViewController,我怎么扔掉的对象和/或保存在这一点?
因此,上下文消失并且仅在该方法所在的位置创建对象时才是重要的?我仍然可以传递这个对象,并且仍然可以访问临时的上下文,即使它不是伊娃或什么的? – Crystal
是的,确切地说。如上所述,您可以从对象中获取上下文。 – Mundi
嗯,这似乎并非如此。一旦tempContext失去作用域,即使我有NSManagedObject,managedObjectContext属性也是null。 – Crystal