1

多くのエンティティ「B」を含むエンティティ「A」があります。

アイテム「B」のリストを取得したい:

Aには「b_list」と呼ばれるBのNSSetが含まれています

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);
        }
    }

オブジェクトのリストではなく、障害の結果が返される理由がわかりません。

4

1 に答える 1

3

Bが空の場合、フォールトを返します。forループ内のコードが表示されないため、空であることに強い賭けがあります。

于 2012-11-26T16:06:47.253 に答える