Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data NSFetchRequest setResultType:NSDictionaryResultType not working

Tags:

ios

core-data

When setting a NSFetchRequest result type to NSDictinaryResultType, zero objects are returned. If I remove setPropertiesToFetch and setResultType, all the objects are returned.

Any ideas?

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alert" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];

NSDictionary *entityProperties = [entity propertiesByName]; 
[request setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"test"]]];
[request setResultType:NSDictionaryResultType];

NSError *error;
NSArray *result = [_managedObjectContext executeFetchRequest:request error:&error];

if (result == nil) {
    NSLog(@"Error: %@", [error localizedDescription]);
}
like image 313
bbarnhart Avatar asked Aug 11 '11 00:08

bbarnhart


1 Answers

The problem was I needed to save the mangedObjectContext after inserting new objects. Once I did that I get the results I was looking for.

like image 79
bbarnhart Avatar answered Oct 06 '22 21:10

bbarnhart