I have an entity “A” that contains many entity “B”:
I want to retrieve the list of item “B”:
A contains a NSSet of B called “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);
}
}
I don’t understand why it still return a fault result and not the list of objects.
It will return a fault if B is empty. Since your not seeing the code inside of your for loop then it is a strong bet that it is empty.