[英]Core Data: nil managed object context on my managed object
我正在使用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!
它是怎么被刺破的? 我沒有刪除它,這似乎是它抱怨的原因。
我想到了
[context reset];
..被每50條記錄調用一次,這當然是從上下文中刪除了我的metricType對象,而我沒有重新獲取它。
初始化metricType的方式看起來很糟糕。 [self metricTypeForName:fromContext:]實際執行什么操作以返回MetricType實例? 您是否正在使用[[MetricType alloc] init]創建新的MetricType? 我懷疑您正在自動釋放池中返回某些內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.