IPhone core data binding error

I am creating an iphone application for basic data and have problems retrieving one-to-many relationship data. Please feel free to as I explain.

I used the data model constructor to set up an object called Item, which contains many objects called Comment. Then I retrieve a few objects and show them in a UITableView . I take 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]; 

When the user clicks on a row, I select a specific entity, pass it to the new table view controller in my init method, and push the new view controller onto 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]; } 

In the first block, the NSLog output shows that the objects of the relationship "Item_to_item_comments" were restored, and in the second even if I called [request setRelationshipKeyPathsForPrefetching: [NSArray arrayWithObject: @ "Item_to_item_comments"]].

Here is part of the first NSLog release:

 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})", 

You can see that Item_to_item_comments objects are being retrieved, 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; }) 

Now Item_to_item_comments has an error. Similarly, in the controller with the pressed view, the Items object is passed, but Item_to_item_comments not.

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

Any help would be appreciated.

Peter

+4
source share
1 answer

An “error” does not mean an error, it simply means that the returned object is a “ghost,” without reading its attributes. It’s normal to get errors for the other side of the relationship, because you don’t have what fetch to run the uncontrolled cascade of creating an object through its relationship.

When you access the error attribute, it will crash as a fully functional object.

Edit:

From the comment:

The problem is that I request such a relationship through NSLog, but I still cannot get entity relationships.

No no. You simply query the Items objects themselves, and then register them. They return errors for their relationship as expected. Only if you ask everyone about the actual object on the other side of the relationship, you are guaranteed to see the object, not the error.

This is what you need to cause an error in the objects with respect to:

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

Another problem is that you are comparing the results of two separate samples. It:

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

... logs mutableItemsFetchResults until when fetching occurs. The following NSLog (presumably) logs the results after extraction. This means that you are viewing, possibly, two different sets of objects in two different fault states.

You may also have a problem because mutableItemsFetchResults seems to be a property, but you do not use the self.mutableItemsFetchResults notation to make sure it is saved correctly. Also, I don't think you need mutableCopy .

+9
source

Source: https://habr.com/ru/post/1313952/


All Articles