我四处寻找人们如何使用核心数据进行添加和编辑。我遇到了 WWDC 2012 - 214 视频以及这篇文章: http: //www.cocoanetics.com/2012/07/multi-context-coredata/,它谈到了与父母一起使用子上下文。我的问题是,如果我需要在一个 ViewController 上创建一个临时对象,然后将该临时对象传递给几个 viewController 的深处,直到我决定是否要保存,该怎么办。然后我需要通过 viewControllers 将 tempContext 传递给我吗?或者我只需要将 NSManagedObject 传递给其他 viewController 并且 tempContext 是方法的局部变量并不重要。例如:
ViewController 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 中,此时我该如何丢弃和/或保存它?