我四处寻找人们如何使用核心数据添加vs编辑。我偶然发现了WWDC 2012 - 214视频以及这篇文章:http://www.cocoanetics.com/2012/07/multi-context-coredata/,其中讨论了如何在父视图中使用子上下文。我的问题是,如果我需要在一个ViewController上创建一个临时对象,然后将该临时对象传递给几个viewController,直到我决定是否要保存。我是否需要通过viewControllers传递tempContext?或者我只需要将NSManagedObject传递给其他viewControllers,并且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中,如何丢弃对象和/或保存它?
答案 0 :(得分:0)
只需传递对象即可。您可以使用
访问对象中的上下文(无论是子项还是主项)object.managedObjectContext