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!

-------------Problems Reply------------

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?

You're right, there isn't an "easy" way to do this with Core Data, because Core Data is not a database. However, it is possible. Here's the general idea:

  1. Fetch all your Dealer objects via an NSFetchRequest. To simplify the query, you can set the fetch request to only fetch the city attribute.
  2. Execute NSArray * uniqueCities = [fetchedDealers valueForKeyPath:@"@distinctUnionOfObjects.city"];

A quick way to ensure a unique set of things is to use NSSet. Once you have the results for a query on city take your NSArray and do

NSSet* uniqueResults = [NSSet setWithArray:resultsArray];

You can transform the set into another collection class if more convenient or just the object enumerator to do something with all of them. I do not know if this approach or the valueForKeyPath method from Dave DeLong is more efficient.

Category:iphone Views:0 Time:2010-03-22

Related post

  • Uniquing with Existing Core Data Entities 2010-05-29

    I'm using Core Data to store a lot (1000s) of items. A pair of properties on each item are used to determine uniqueness, so when a new item comes in, I compare it against the existing items before inserting it. Since the incoming data is in the form

  • Adding unique objects to Core Data 2009-08-05

    I'm working on an iPhone app that gets a number of objects from a database. I'd like to store these using Core Data, but I'm having problems with my relationships. A Detail contains any number of POIs (points of interest). When I fetch a set of POI's

  • Retrieve unique results with Core Data, NSPredicate? 2010-04-16

    I need to be able to do the equivalent of the following SQL using Core Data: SELECT DISTINCT(foo) FROM bar Is there a way of doing this with NSPredicate? Or any other way for that matter? --------------Solutions------------- I believe all you need to

  • iPhone SDK: Core Data and uniquing? 2010-07-14

    Either I'm not understanding what the term "uniquing" means in Core Data, or I'm not fetching my data properly. I have a pretty simple data model. Three entities: Community, Asset, and Category. Each Community has a relationship to multiple categorie

  • How do i set up a core data model and with what relationships in this case? 2014-09-19

    I am trying to learn some Core Data, and i have limited knowledge in general when it comes to databases (which i know Core Data is not, it's an 'Object Graph Manager') so i am in somewhat deep need of some help to figuring out what exactly i need to

  • Creating a unique id for a Core Data program on the iPhone 2009-08-29

    I am having a bit of trouble figuring out this Core Data stuff. How do I create a new entry with a unique ID? In SQL I would just declare one field as an autoincrement field. I'm not seeing anything like that here, but I could just be missing somethi

  • Core Data - Getting Unique Rows 2010-01-03

    I'm working on an iPhone app that uses Core Data. I have only one entity called Books, and that entity has the attributes Title, Author, ISBN, Description and Shelf. I'm working on the book editing view, and want to have a UIPickerView listing all of

  • Thread-safe unique entity instance in Core Data 2010-02-02

    I have a Message entity that has a messageID property. I'd like to ensure that there's only ever one instance of a Message entity with a given messageID. In SQL, I'd just add a unique constraint to the messageID column, but I don't know how to do thi

  • Core Data unique attributes 2010-02-10

    Is it possible to make a Core Data attribute unique, i.e. no two MyEntity objects can have the same myAttribute? I know how to enforce this programatically, but I'm hoping there's a way to do it using the graphical Data Model editor in xcode. I'm usi

  • Can I tell Core Data to use a specific unique ID for an y object when saving it? 2010-02-12

    Example: I read data from an XML file. This data has unique id elements. I want to store those objects with their original unique id. How would I do that? I figured out I could ask the managed object for it's ID, like this: NSManagedObjectID *moID =

  • Core Data uniqueness 2010-03-10

    Is there any way I can validate a value updated in a Core Data entity's property against values of the property in other entities in the collection? At the moment I create an entity with some default values, add it to arrangedObjects, then get the us

  • NSArrayController that is sorted and unique (no duplicates) for use in a pop-up in a core-data app 2010-05-08

    I have core data app with an entity OBSERVATION that has as one of its attributes DEALNAME. I want to reference through Interface Builder or by making custom modifications to an NSArrayController a list of unique sorted dealnames so that I can use th

  • Core Data - Unique Values Within a Scope 2010-06-24

    I am new to Core Data and am trying to figure out how to set up a validation. I am tracking apartment buildings, where each Building entity has one or more Apartment entities. Each Apartment has a unitNumber property, which is unique within the scope

  • Core Data - How to validate the uniqueness of an attribute within the scope of 2010-07-17

    I would like to know how to implement a validation in Core Data. What I'd like to do is ensure that an attribute is unique within the scope of a related parent object. In other words, I'm wondering how to implement the validates_uniqueness_of :field,

  • Core Data editing attributes and getting at the unique object to edit 2010-07-18

    I'm coming from SQL and n-tiered systems and trying to work through how to use Core Data effectively for this usage: insert new managed object - I'm having no problem there delete existing managed object - I'm having no problem there editing existing

  • Find or create unique Core Data entity 2011-02-11

    A common scenario for core data driven apps is getting a unique object out of the backing store. If the object with a certain unique property exists, return that one, if it doesn't return a newly create one. I found myself writing the same thing over

  • iOS Core Data unique object fetch 2011-09-02

    I'm fairly new to iOS/Cocoa and I have a very fundamental question about core data. I was looking over the internet to find a appropriate answer/solution but I wasn't able to. So the question is how do you handle uniqueness in core data? I know that

  • Unique Identifier for iOS core data object? 2011-10-23

    I have an iPhone app using Core Data, but I can't seem to figure out a solid unique identifier to a particular entity. Here is the situation: I have a Profile entity, with relationships to other entities. At the start of the app, the user with select

  • concurrent saving from two different threads to Core Data persistant store with unique entity Id 2011-12-15

    I'm implementing multithreaded core data downloader. I have a problem with doubling objects while saving objects with unique string attribute in Entity. If 2 threads are downloading from the same url simultaneously (f.e., updater-timer fires and appl

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.088 (s). 11 q(s)