我已经处理这个问题一段时间了(https://stackoverflow.com/questions/12982012/unit-testing-core-data-to-many-relationship),但我终于能够辨别出什么真正的问题是。无论我尝试做什么,我都有一个始终在主 managedObjectContext 上创建的实体。我的所有其他实体都工作正常,但这个实体似乎特别有问题。自从我用新名称提出上一个问题后,我已经重建了整个数据模型,但问题仍然存在。我尝试搜索并看到一个问题(为什么我的一些核心数据对象已将 managedObjectContext 设置为 nil?)引用了一个类似的问题,他被设置为 nil。
我正在尝试做的是为导入数据以在后台运行创建一个新的上下文。
以下是解释一切的日志
首先是[self managedObjectContext]。我正在使用它从 AppDelegate 中调用 MOC,它的代码直接来自基于 Apple 的 Core Data 应用程序。
- (NSManagedObjectContext *)managedObjectContext
{
return [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
在 LoginVC 中使用 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>
在我的更新类中使用多个 managedObjectContexts。importContext2 只是我为测试目的而提出的一个。
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>