Adrian Ilie
Adrian Ilie

Reputation: 133

fetching CoreData entity relation as NSDictionary

my model looks like this: Valid XHTML

i am trying to fetch an array of articles (as NSDictionary) and also include their images. my code looks like this:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Article"];
request.resultType = NSDictionaryResultType;
request.sortDescriptors = @[[[NSSortDescriptor alloc] initWithKey:@"date_publish" ascending:NO]];
NSArray *articles = [[CoreData sharedInstance].backgroundContext executeFetchRequest:request error:nil];

however the resulting array does not contain anything about the article images. i also tried to use an NSExpressionDescription

NSExpressionDescription *imageExpression = [[NSExpressionDescription alloc] init];
imageExpression.name = @"image";
imageExpression.expression = [NSExpression expressionForEvaluatedObject];
imageExpression.expressionResultType = NSUndefinedAttributeType;

request.propertiesToFetch = @[@"article_id", @"title", @"content_lead", @"content_full", @"date_create", @"date_update", @"date_publish", imageExpression];

now i can see something like this in the console

image = "0xd000000004040002 <x-coredata://1F512858-AAF2-4546-9646-3395CA182CFE/Article/p257>";

this is not very useful, it should be an array consisting of NSDictionaries.

how can i fetch an entity relationships as NSDicionaries ?

Upvotes: 0

Views: 135

Answers (1)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

You need to tell the fetch to include the relationship using relationshipKeyPathsForPrefetching. However, I am not certain that will work with a dictionary return type but it is worth testing.

You might only be able to pre-fetch the object on the other side of the relationship as NSManagedObject instances. If that is the case then you will want to file a radar with this issue.

Upvotes: 1

Related Questions