Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique Values from Core Data

I have a core data-based app that manages records of auto dealerships. Each record stores the dealer's address, which is broken into addressLine1, addressLine2, city, state, and zip components, each stored as a string in the data store.

I would like to present a list of cities with dealerships to the user, so I'm trying to figure out if it is possible to get a list of every unique city name that has been entered into the store. I other words, is it possible to issue some sort of query against all of the dealership records that will return a list of distinct city names?

I would know how to do this easily with a SQL query, but (how) is this done in Core Data?

Thanks very much!

like image 612
John Stupak Avatar asked Dec 17 '22 02:12

John Stupak


1 Answers

Core Data have the option to get distinct record. The method of getting unique results using NSArray and NSSets are not recommend.

[fetchRequest setResultType:NSDictionaryResultType];
NSDictionary *entityProperties = [entity propertiesByName];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[entityProperties objectForKey:@"<<yourattrib>"]]];
[fetchRequest setReturnsDistinctResults:YES];

Refer Apple documentation and check the answers for How to fetch distinct values in Core Data?

like image 108
palaniraja Avatar answered Dec 30 '22 12:12

palaniraja