I’m new to core data and having some difficulty finding any information about sorting and only fetching non duplicates of a specified attribute.
I have a list of locations and they all have different addresses but some have the same city. i would like to fetch all the cities excluding duplicate cities and in alphabetical order.
Would the best way be to have 2 attributes, one for city and another for locationDetails. in the city attribute it will just have a list of cities with no duplicates and when selecting a city it will fetch all the locationDetails for that city attribute?
Thanks,
Yes the best way will be to have a separate attribute for city. And you don’t need to structure your entity as unique. You can take care of sorting and fetching unique in your fetch request.
Note: This request returns an array of dictionaries with City as its key. Don’t forget to release the NSFetchRequest after executing the fetch.
EDIT
When the user clicks on a city, store it in some variable (cityName). Now execute another fetch request like:
You should do this with the same entity. This will fetch only the objects whose city name has been selected. Now, you can either do the fetch in 2 ways:
Fetch the entire entities and then dispay using the format
entityName.Location. In this case you will get an NSArray ofobjects
Set the properties to fetch like:
In the second case, it returns a NSDictionary ( Don’t forget to set Result Type as NSDictionaryResultType as before ).