1

2つのエンティティ間に1対多の関係があり、ユーザーが親エンティティを選択すると、NSFetchedResultsControllerを使用してサブカテゴリが表示されます。私のフェッチコードを以下に示します。エラーを見ると、データの入力中に問題があるようです。だから私もダミーの挿入コードを付けていました。

NSFetchResultControllerを使用して親エンティティの子にしようとすると、以下のエラーが発生します

エラーメッセージ キャッチされなかった例外のためにアプリを終了しています'NSInvalidArgumentException'、理由:'キーパスの親がエンティティに見つかりませんNSSQLEntityCategoryDe​​tail id = 2'

- (void)addData:(NSArray *)array forCategory:(NSArray *)a{
    //  if(target_iphon)



    MouseWaitAppDelegate *delegate=(MouseWaitAppDelegate *)[[UIApplication sharedApplication]delegate];
    NSManagedObjectContext *context = [delegate managedObjectContext];
    Categories *category1 = [NSEntityDescription
                                       insertNewObjectForEntityForName:@"Categories"
                                       inManagedObjectContext:context];

    category1.serverId=[NSNumber numberWithInteger:[[a objectAtIndex:0]integerValue]];
    category1.name=[a objectAtIndex:1];
    category1.textWidth=[NSNumber numberWithFloat:35.0f];


    for (NSDictionary *dic in array)
    {
        CategoryDetail *cDetail = [NSEntityDescription
                                   insertNewObjectForEntityForName:@"CategoryDetail"
                                   inManagedObjectContext:context];

        if ([[dic objectForKey:@"type"] isEqualToString:@"Tabs"])
        {


            cDetail.serverId=[dic objectForKey:@"ID"];
            cDetail.name=[dic objectForKey:@"name"];
            cDetail.desc=[dic objectForKey:@"desc"];
            cDetail.type=[dic objectForKey:@"type"];
            cDetail.orderNo=[NSNumber numberWithInteger:[[dic objectForKey:@"order_no"]integerValue]];

        }
        else if ([[dic objectForKey:@"type"] isEqualToString:@"events"])
        {
            cDetail.serverId=[dic objectForKey:@"ID"];
            cDetail.name=[dic objectForKey:@"name"];
            cDetail.latitude=[dic objectForKey:@"lat"];
            cDetail.longitude=[dic objectForKey:@"long"];
            cDetail.subCatId=[dic objectForKey:@"subcatid"];
            cDetail.desc=[dic objectForKey:@"desc"];
           // cDetail.event_inst_date=[NSData da]
            cDetail.type=[dic objectForKey:@"type"];
            cDetail.orderNo=[NSNumber numberWithInteger:[[dic objectForKey:@"order_no"]integerValue]];
        }


        cDetail.category=category1;
        [category1 addCategoryDetailObject:cDetail];


    }

    NSError *error;
    if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
    }
}




#pragma mark - Getter Setter OverRide
- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"CategoryDetail" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    //NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"details.closeDate" ascending:NO];

    if (self.parentCategory!=nil)
    {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parent == %@", self.parentCategory];
        [fetchRequest setPredicate:predicate];
    }


    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"orderNo" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:nil
                                                   cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}
4

1 に答える 1

5

私があなたのコードを正しく理解していれば、CategoryDe​​tailからCategoryへの関係呼び出され、ではありません。したがって、述語categoryparent

[NSPredicate predicateWithFormat:@"parent == %@", self.parentCategory];

する必要があります

[NSPredicate predicateWithFormat:@"category == %@", self.parentCategory];
于 2013-03-02T14:56:37.477 に答える