我在两个实体之间有一对多的关系,当用户选择父实体时,我使用 NSFetchedResultsController 显示子类别。下面给出了我的获取代码。通过看到错误,问题似乎是在输入数据时。所以我也附上了虚拟插入代码。
当我尝试使用 NSFetchResultController 的父实体的子实体时,它给出了以下错误
由于未捕获的异常'NSInvalidArgumentException',错误消息终止应用程序,原因:'keypath parent not found in entity NSSQLEntity CategoryDetail 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;
}