由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'在实体NSSQLEntity中找不到keypath parentDetail id = 2

时间:2013-03-02 13:18:17

标签: iphone ios objective-c core-data

我有两个实体之间的一对多关系,当用户选择父实体时,我正在使用NSFetchedResultsController显示子类别。我的提取代码如下。 通过查看错误,问题似乎是在分配数据时。所以我也附上了虚拟插入代码。

当我尝试使用NSFetchResultController进行父实体的子操作时,它给出了以下错误

错误消息 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'在实体NSSQLEntity中未找到keypath parentDetail 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;

}

1 个答案:

答案 0 :(得分:5)

如果我正确理解您的代码,则从 CategoryDe​​tail Category 的关系称为category而不是parent。因此谓词

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

应该是

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