1
votes

Can't seem to figure out the trick to fetch an attribute from a relationship when limiting the fetch to a number of properties. In the code below "thumbnail" is a transformable UIImage attribute of the entity "Photo". That always comes back fine in the array of dictionaries with the key "thumbnail". What I cannot figure out is the second property/attribute where "previewImage" is a relationship in Photo to another entity named "Image" that has a transformable attribute "uiImage" (which is a UIImage).

I've tried this:

[fetchRequest setPropertiesToFetch:@[@"thumbnail", @"previewImage.uiImage"]];

but I got this error:

-[NSExpressionDescription valueTransformerName]: unrecognized selector sent to instance 0x13078920

and I've tried this:

[fetchRequest setPropertiesToFetch:@[@"thumbnail", @"previewImage"]];

where I do get two items in each dictionary

{
   previewImage = "0xbabba90 <x-coredata://03FF7129-1345-4BAE-A74E-2FF5A2B38CDC/Image/p5>";
   thumbnail = "<UIImage: 0xbabbc60>";
}

but when I try to use that "Image" object as below

NSDictionary *dict = [self.photos objectAtIndex:0];
Image *theImage = (Image *)[dict objectForKey:@"previewImage"];
[self.delegate photoCollectionViewController:self didSelectPreviewImage:theImage.uiImage];

I get this error

"[_NSObjectID_48_0 uiImage]: unrecognized selector sent to instance 0xbabba90"

And I've even thrown this in the request

[fetchRequest setRelationshipKeyPathsForPrefetching:@[@"previewImage.uiImage"]];

So, I guess I'm flailing and not getting this. Any help would be appreciated. Thanks!

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Photo" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];
1
The reference to _NSObjectID_48_0 suggests that you used NSManagedObjectIDResultType somewhere, though your code says NSDictionaryResultType. Did you ask for object IDs somewhere? - Tom Harrington
No. I searched my entire project for "NSManagedObjectIDResultType" and nothing there. I didn't even know you could change the result type until I tried to limit fetched properties :) BTW, I have your book ;) - user1139479

1 Answers

1
votes

With the NSDictionaryResultType you can conveniently fetch the properties of an entity. It is not meant for traversing relationships. That is why you get an error when you do what would be logical: to fetch previewImage.uiImage.

From the docs:

The property descriptions may represent attributes, to-one relationships, or expressions.

Maybe you are still thinking in terms of a database query, where you selectively choose the "fields" to fetch. This is not necessary in Core Data, which is an object graph, not a database.

Instead, just fetch your NSManagedObjects. If you are worried about memory, you shouldn't be. Core data uses a mechanism called faulting to fetch only what it needs. Indeed the above cryptic "<x-coredata://03FF7129-1345-4BAE-A74E-2FF5A2B38CDC/Image/p5>" is pretty similar to how a fault wold be displayed in the console.

Try to do your fetch with normal entities. You can expect it to "just work".