开发者

Core Data: nil managed object context on my managed object

I'm using 2 managed object contexts for efficiently important a large data set in the background. I'm ensuring I'm only using 1 managed object context at a time in the thread.

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];

In the snipped of code above, the NSLog correctly prints out the address of the managedObjectContext i'm using. I then go on to processDataInContext - which is just a private method to interate over a json data array and add objects. Each object h开发者_开发问答as a relationship to the MetricType.

However, when I go to associate them

metric.metricType = metricType;

I get the error: Illegal attempt to establish a relationship 'metricType' between objects in different contexts.... even though I'm ensuring I don't do this.

When I do a log output before this line:

NSLog(@"My Context %@", context);
NSLog(@"metric context %@", [metricType managedObjectContext]);

The metricType context returns nil!!

How has it become nilled? I didn't nil it and this seems to be the reason its complaining.


I figured it out

[context reset]; 

..was being called every 50 records, which of course was removing my metricType object from the context and I wasn't re-fetching it.


The way you initialize metricType looks fishy. What does [self metricTypeForName:fromContext:] actually do to return a MetricType instance? Are you creating a new MetricType using [[MetricType alloc]init]? I suspect you are returning something in an auto-release pool

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜