我一直处理这个问题已经有一段时间了(https://stackoverflow.com/questions/12982012/unit-testing-core-data-to-many-relationship),但我终于能够看出真正的问题是什么了。无论我尝试做什么,我都会在主要的managedObjectContext上创建一个单独的实体。我所有的其他实体都运行良好,但特别是这个实体似乎有问题。我重建了整个数据模型,因为我用新名称问了上一个问题,问题仍然存在。我尝试搜索并看到一个问题(Why do some of my core data objects have managedObjectContext set to 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>
答案 0 :(得分:0)
为了自己回答这个问题,以防万一有人犯了错误。但是我所有的NSManagedObject子类都为它们创建了类别,并且在某些时候我天真地放了
- (NSManagedObjectContext *)managedObjectContext
{
return [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
在我的类别中。我正在制作另一种方法来执行自定义提取请求,并认为我需要它来访问MOC,我显然不这样做。实现它是完全可以接受的,因此它被隐藏在类别中并被遗忘。因此,只要我的Deal对象试图访问它的MOC,那么调用被覆盖并且正在返回主MOC。