我有两个上下文:parentContext和childContext。当我尝试将objectContext中的objectID传递给parentContext时,不会为parentContext中的to-many关系触发错误。
to-many关系称为“类别”,是无序的(NSSet)。
NSManagedObjectContext *parentContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = parentContext;
childContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
NSSet *childObjects = [childContext registeredObjects];
[childContext save:NULL];
[parentContext performBlockAndWait:^{
for (NSManagedObject *childObject in childObjects) {
NSManagedObject *parentObject = [parentContext objectWithID:childObject.objectID];
// [parentObject hasFaultForRelationshipNamed:@"categories"]; returns false
// [childObject hasFaultForRelationshipNamed:@"categories"]; // returns true as expected
[parentContext refreshObject:parentObject mergeChanges:YES];
}
}];
即使在parentContext中,我也希望“类别”关系出错。你能帮忙吗?
答案 0 :(得分:0)
我想你还必须保存父上下文。子上下文仅保存"推送"更改直到父上下文,但此时它们不会保存到商店。
保存父上下文后,您的错误将会触发。