iPhone Core数据关系故障 [英] iPhone Core Data relationship fault

查看:170
本文介绍了iPhone Core数据关系故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个核心数据iphone应用程序,并且在检索一对多关系数据时遇到问题。

I am building a core data iphone app, and having trouble with retrieving one-many relationship data. Please bear with me while I explain.

我使用数据模型设计器来设置一个名为Item的实体,该实体包含许多名为Comment的实体。然后我检索多个实体,并在 UITableView 中显示它们。我获取这样的实体(在 viewDidLoad 方法):

I have used the data model designer to setup an entity called "Item" that contains many entities called "Comment". I then retrieve multiple entities and display them in a UITableView. I fetch these entities like this (in the viewDidLoad method):

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];

当用户点击一行时,我选择特定的entiny,传递给一个新的表视图控制器在其init方法中,并将新的视图控制器推送到堆栈:

When the user taps on a row, I select the particular entiny, pass it to a new table view controller in its init method, and push the new view controller to the stack:

- (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关系实体检索,但在第二,即使我调用[请求setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@Item_to_item_comments]]。

In the first block the NSLog output shows that the "Item_to_item_comments" relationship entities were retrieved, but in the second, that it wasn't even though I invoked [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"Item_to_item_comments"]].

这是第一个NSLog输出:

Here is part of the first NSLog output:

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:

You can see that the Items entities are fetched, including Item_to_item_comments. Here's the second 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;

})

$ b b

现在, Item_to_item_comments 是故障。同样,在推送视图控制器中,Items实体被传递,但 Item_to_item_comments 不是。

Now, Item_to_item_comments is fault. Similarly, in the pushed view controller, the Items entity is passed, but Item_to_item_comments is not.

我缺少明显的东西,但一天花在这个问题后,我不能想出来。

I think I am missing something obvious, but after a day spend on this problem, I can't figure it out.

任何帮助都会感激。

彼得

推荐答案

fault并不意味着一个错误,它只是意味着返回的对象是一个没有其属性读入的ghost。为另一方获取错误是正常的的关系,因为你没有什么提取来引发一个不受控制的对象创建的级联通过其关系。

A "fault" doesn't mean an error, it just means that the object returned is a "ghost" without its attributes read in. It's normal to get faults for the other side of relationship because you don't what a fetch to set off an uncontrolled cascade of object creation via its relationships.

当您访问故障的属性时,它将作为完全功能的对象被错误处理。

When you access an attribute of the fault, it will be faulted in as a fully functional object.

编辑:

来自评论:


问题是, b $ b这种关系通过NSLog,但是I
仍然不能得到关系
实体。

The problem is that I am requesting such relationship through NSLog, but I still can't get the relationship entities.

不你不是。你只是请求 Items 实体,然后记录它们。他们正如预期的那样为他们的关系返回错误。只有当你向每个人询问关系另一方的实际对象时,你才能保证看到一个对象而不是一个错误。

No you're not. Your just requesting the Items entities themselves and then logging them. They are returning faults for their relationships as expected. Only if you ask each for the actual object on the other side of the relationship are you guaranteed to see an object instead of a fault.

这是您需要强制关系中的对象的故障切入:

This is what you need to force the faulting-in of the objects in the relationship:

NSLog(@"itemObject.Item_to_item_comments: %@", [mutableItemsFetchResults objectAtIndex:indexPath.row].Item_to_item_comments.someAttribute]);

另一个问题是你正在比较两个独立提取的结果。这:

You're other problem is that you are comparing the results of two seperate fetches. This:

NSLog(@"Results: %@", [mutableItemsFetchResults description]);
mutableItemsFetchResults = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];

...记录 mutableItemsFetchResults 之前发生提取。下一个NSLog(大概)在提取后记录结果。这意味着您在两种不同的故障状态下查看可能的两组不同的对象。

... logs the mutableItemsFetchResults before the fetch occurs. The next NSLog (presumably) logs the results after the fetch. This means you're looking at possibly two different sets of objects in two different fault states.

您可能也有问题,因为 mutableItemsFetchResults 显然是一个属性,但您不使用 self.mutableItemsFetchResults 符号,以确保它被正确保留。另外,我不认为你需要 mutableCopy

You may also have a problem because mutableItemsFetchResults is apparently a property but you're not using the self.mutableItemsFetchResults notation to make sure it is properly retained. Also, I don't think you need themutableCopy.

这篇关于iPhone Core数据关系故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆