我正在构建一个核心数据 iphone 应用程序,但在检索一对多关系数据时遇到了麻烦。请耐心等待我解释。
我使用数据模型设计器设置了一个名为“Item”的实体,其中包含许多名为“Comment”的实体。然后我检索多个实体并将它们显示在UITableView
. 我像这样(在viewDidLoad
方法中)获取这些实体:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Items" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Item_to_Areas.Name LIKE %@)",[areaManagedObject valueForKey:@"Name"]];
[request setPredicate:predicate];
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"Item_to_item_comments"]];
NSLog(@"Results: %@", [mutableItemsFetchResults description]);
mutableItemsFetchResults = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
[request release];
当用户点击一行时,我选择了特定的实体,在它的 init 方法中将它传递给一个新的表视图控制器,并将新的视图控制器推送到堆栈中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"itemObject: %@", [mutableItemsFetchResults objectAtIndex:indexPath.row]);
InspectionItemCommentsViewController *itemCommentsViewController = [[InspectionItemCommentsViewController alloc]
initWithManagedObjectContext:self.managedObjectContext
itemObject:[mutableItemsFetchResults objectAtIndex:indexPath.row]];
itemCommentsViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:itemCommentsViewController animated:YES];
[itemCommentsViewController release];
}
在第一个块中,NSLog 输出显示“Item_to_item_comments”关系实体已被检索,但在第二个块中,即使我调用了 [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"Item_to_item_comments"]],它也没有。
这是第一个 NSLog 输出的一部分:
Results: (
"<NSManagedObject: 0xb356b70> (entity: Items; id: 0xb34fe60 <x-coredata://E43A90B5-AF0E- 4394-B4A7-5EFE74E181F8/Items/p1> ; data: {\n Clean = nil;\n Description = \"\";\n ItemToInformation = \"<relationship fault: 0x5e1ef00 'ItemToInformation'>\";\n \"Item_to_Areas\" = \"0xb33fd30 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Areas/p1>\";\n \"Item_to_item_comments\" = \"<relationship fault: 0x5e1d300 'Item_to_item_comments'>\";\n Keys = nil;\n Name = Other;\n \"Tenant_agrees\" = nil;\n Undamaged = nil;\n Working = nil;\n})",
"<NSManagedObject: 0xb35a930> (entity: Items; id: 0xb32acc0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Items/p2> ; data: {\n Clean = nil;\n Description = \"\";\n ItemToInformation = \"<relationship fault: 0x790de40 'ItemToInformation'>\";\n \"Item_to_Areas\" = \"0xb33fd30 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Areas/p1>\";\n \"Item_to_item_comments\" = (\n \"0xb35bcb0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p13>\",\n \"0xb35bcd0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p37>\",\n \"0xb35bca0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p5>\",\n \"0xb35bcc0 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Item_Comments/p26>\"\n );\n Keys = nil;\n Name = Lights;\n \"Tenant_agrees\" = nil;\n Undamaged = nil;\n Working = nil;\n})",
您可以看到 Items 实体已获取,包括Item_to_item_comments
. 这是第二个 NSLog:
itemObject: <NSManagedObject: 0xe20af50> (entity: Items; id: 0xe209fc0 <x- coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Items/p2> ; data: {
Clean = nil;
Description = "";
ItemToInformation = "<relationship fault: 0xe302e40 'ItemToInformation'>";
"Item_to_Areas" = "0xe500b90 <x-coredata://E43A90B5-AF0E-4394-B4A7-5EFE74E181F8/Areas/p1>";
"Item_to_item_comments" = "<relationship fault: 0xe302e60 'Item_to_item_comments'>";
Keys = nil;
Name = Lights;
"Tenant_agrees" = nil;
Undamaged = nil;
Working = nil;
})
现在,Item_to_item_comments
是错。同样,在推送的视图控制器中,Items 实体被传递,但Item_to_item_comments
不是。
我想我错过了一些明显的东西,但是在这个问题上花了一天时间之后,我想不通。
任何帮助,将不胜感激。
彼得