我有一个包含许多实体“B”的实体“A”:
我想检索项目“B”的列表:
A 包含 B 的 NSSet,称为“b_list”
NSEntityDescription *selectEntityDescription = [NSEntityDescription
entityForName:@"A" inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:selectEntityDescription];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"B"]];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSPredicate *whereForFetch = [NSPredicate predicateWithFormat:@"id == %@", object_id];
[fetchRequest setPredicate:whereForFetch];
NSError *error = nil;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
A *a = nil;
if (array != nil && [array count] > 0){
NSLog(@"A exist"); //return "A exist"
a = [array objectAtIndex:0];
NSLog(@"a b_list %@",a.b_list); //return a Fault result
NSLog(@"a b_list count %d",a.b_list.count); //return 0
for (B *b in a.b_list){ //not executed
NSLog(@"b %@",b.name);
}
}
我不明白为什么它仍然返回错误结果而不是对象列表。