Core data 使用setRelationshipKeyPathsForPrefetching获取CoreData中的故障关系对象

Core data 使用setRelationshipKeyPathsForPrefetching获取CoreData中的故障关系对象,core-data,nsfetchrequest,fault,Core Data,Nsfetchrequest,Fault,我有一个实体“A”,其中包含许多实体“B”: 我想检索项目“B”的列表: A包含名为“B_列表”的NSB集 我不明白为什么它仍然返回错误结果而不是对象列表。如果B为空,它将返回错误。既然您没有看到for循环中的代码,那么很有可能它是空的。使用“a.b_list@count

我有一个实体“A”,其中包含许多实体“B”:

我想检索项目“B”的列表:

A包含名为“B_列表”的NSB集


我不明白为什么它仍然返回错误结果而不是对象列表。

如果B为空,它将返回错误。既然您没有看到for循环中的代码,那么很有可能它是空的。

使用“a.b_list@count<1”作为谓词检查列表是否为空/错误。从这里开始
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);
        }
    }