【发布时间】:2011-09-14 20:50:04
【问题描述】:
我正在使用 2 个托管对象上下文在后台有效地处理大型数据集。我确保我在线程中一次只使用 1 个托管对象上下文。
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
MetricType *metricType = [self metricTypeForName:self.metricName fromContext:context];
NSLog(@"metric context %@", [metricType managedObjectContext]);
[self processDataInContext:context forMetric:metricType];
在上面的代码片段中,NSLog 正确打印出我正在使用的 managedObjectContext 的地址。然后我继续处理 processDataInContext - 这只是一个私有方法,用于交互 json 数据数组并添加对象。每个对象都与 MetricType 有关系。
但是,当我去关联它们时
metric.metricType = metricType;
我收到错误:非法尝试在不同上下文中的对象之间建立关系“metricType”....即使我确保我不这样做。
当我在此行之前进行日志输出时:
NSLog(@"My Context %@", context);
NSLog(@"metric context %@", [metricType managedObjectContext]);
metricType 上下文返回 nil!!
它是如何变成空的?我没有取消它,这似乎是它抱怨的原因。
【问题讨论】:
标签: ios core-data nsmanagedobjectcontext