我一直在處理這個問題一段時間(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?)引用類似的問題,他被設置爲零。NSManagedObject總是在同一個ManagedObjectContext上創建
我想要做的是創建一個新的上下文導入數據在後臺運行。
下面是會解釋一切
首先是[自managedObjectContext]日誌。我正在使用這種方法從AppDelegate調用MOC,該代碼直接來自基於Apple的Core Data應用程序。
- (NSManagedObjectContext *)managedObjectContext
{
return [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
在LoginVC使用主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>
不確定這是否相關,但「核心數據最佳實踐」WWDC視頻指出,父上下文必須使用隊列併發類型之一,而標準中設置的主要moc並非如此核心數據模板,iirc。 – omz
這是真的(我在其他地方讀過)。事實上,我已將其更改爲'[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];'除此之外,標準核心數據模板 – TheJer