I’ve spent a few hours trying to get a fetch to work. I need to get rid of duplicates so I thought I could follow this guide core-data-how-to-do-a-select-distinct
But It always give me an empty array.
Here’s my code:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"RBTag" inManagedObjectContext:[self context]];
request.entity = entity;
request.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]];
request.returnsDistinctResults = YES;
request.resultType = NSDictionaryResultType;
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSError *error = nil;
NSArray *distincResults = [[self context] executeFetchRequest:request error:&error];
NSLog(@"Result: %@", distincResults);
I get no error. I checked NSPropertyDescription and it do find my property.
If I comment out:
request.resultType = NSDictionaryResultType;
Then I get results but it’s not distinct. =(
Nothing in any guides or in apple docs, but after hard googling I found this post:
bbarnheart
Must save context to persistent store before changing the resultType to NSDictionaryResultType.