我环顾四周,人们可能会如何添加或编辑核心数据。我看到了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中,如何丢掉该对象和/或保存它呢?
只是传递对象。您可以使用以下命令从对象访问上下文(无论是子对象还是主要对象)
object.managedObjectContext
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句