I’ve been dealing with this issue for awhile now ( https://stackoverflow.com/questions/12982012/unit-testing-core-data-to-many-relationship), but I’ve finally been able to discern what the real problem is. I have a single entity that is always being created on the main managedObjectContext no matter what I try to do. All my other entities work fine, but this one in particular seems to have something wrong with it. I have rebuilt the whole data model since I asked the previous question with a new name and the issue still came back. I tried searching and saw one question ( Why do some of my core data objects have managedObjectContext set to nil?) referencing a similar issue, his were being set to nil.
What I’m trying to do is make a new context for importing the data to run in the background.
Here are the logs that will explain everything
First is the [self managedObjectContext]. I am using this to call the MOC out of the AppDelegate, which has the code straight from an Apple based Core Data app.
- (NSManagedObjectContext *)managedObjectContext
{
return [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
In LoginVC using main managedObjectContext
Deal *testdeal = [NSEntityDescription insertNewObjectForEntityForName:@"Deal" inManagedObjectContext:[self managedObjectContext]];
TriageAnswer *testtriage = [NSEntityDescription insertNewObjectForEntityForName:@"TriageAnswer" inManagedObjectContext:[self managedObjectContext]];
NSLog(@"moc is %@", [self managedObjectContext]);
NSLog(@"testdeal.moc = %@", testdeal.managedObjectContext);
NSLog(@"testtriage.moc = %@", testtriage.managedObjectContext);
2012-10-30 17:51:19.134 myApp[6507:11603] moc is <NSManagedObjectContext: 0x84ace20>
2012-10-30 17:51:19.134 myApp[6507:11603] testdeal.moc = <NSManagedObjectContext: 0x84ace20>
2012-10-30 17:51:19.134 myApp[6507:11603] testtriage.moc = <NSManagedObjectContext: 0x84ace20>
In my update class using multiple managedObjectContexts. importContext2 is just one I came up with for testing purposes.
NSManagedObjectContext *importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[importContext setParentContext:[self managedObjectContext]];
[importContext setUndoManager:nil];
NSManagedObjectContext *importContext2 = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];
[importContext2 setParentContext:importContext];
Deal *importDeal = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Deal class]) inManagedObjectContext:importContext];
Deal *importDeal2 = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Deal class]) inManagedObjectContext:importContext2];
Deal *importDeal3 = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Deal class]) inManagedObjectContext:[self managedObjectContext]];
TriageAnswer *importTriage = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([TriageAnswer class]) inManagedObjectContext:importContext];
NSLog(@"importcontext is %@", importContext);
NSLog(@"importcontext 2 is %@", importContext2);
NSLog(@"self managedobjectcontext is %@", [self managedObjectContext]);
NSLog(@"deal is %@", importDeal.managedObjectContext);
NSLog(@"deal2 is %@", importDeal2.managedObjectContext);
NSLog(@"deal3 is %@", importDeal3.managedObjectContext);
NSLog(@"triage moc is %@", importTriage.managedObjectContext);
2012-10-30 17:54:16.571 myApp[6507:11603] importcontext is <NSManagedObjectContext: 0x84b3ce0>
2012-10-30 17:54:20.403 myApp[6507:11603] importcontext 2 is <NSManagedObjectContext: 0x14163890>
2012-10-30 17:54:22.876 myApp[6507:11603] self managedobjectcontext is <NSManagedObjectContext: 0x84ace20>
2012-10-30 17:54:24.296 myApp[6507:11603] deal is <NSManagedObjectContext: 0x84ace20>
2012-10-30 17:54:25.017 myApp[6507:11603] deal2 is <NSManagedObjectContext: 0x84ace20>
2012-10-30 17:54:25.646 myApp[6507:11603] deal3 is <NSManagedObjectContext: 0x84ace20>
2012-10-30 17:56:34.221 myApp[6507:11603] triage moc is <NSManagedObjectContext: 0x84b3ce0>
Going to answer this for myself just in-case anyone ever makes the mistake I did. But all my NSManagedObject subclasses have categories created for them and at some point I had naively put
in my category. I was making another method for doing a custom fetch request and thought that I needed it to access the MOC, which I obviously dont. It is perfectly acceptable to implement that so it was buried inside the category and forgotten about. Thus anytime my Deal object attempted to access it’s MOC that call was being overridden and the main MOC was being returned.