Unique master data values

I have a basic data-based application that manages the records of auto dealers. Each record stores the address of the dealer, which is divided into the components addressLine1 , addressLine2 , city , state and zip , each of which is stored as a string in the data warehouse.

I would like to present a list of cities with representative offices for the user, so I'm trying to find out if it is possible to get a list of each unique city name that was entered into the store. In other words, is it possible to issue some kind of request against all records of dealerships that return a list of different cities?

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

Many thanks!

+4
source share
3 answers

Master data has the ability to get an excellent record. The method of obtaining unique results using NSArray and NSSets is not recommended.

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

Consult Apple documentation and check the answers. How to get different values ​​in Core Data?

+7
source

You are right, there is no easy way to do this with Core Data, because Core Data is not a database. However, this is possible. Here's a general idea:

  • Retrieve all of your dealer’s objects with NSFetchRequest . To simplify the query, you can configure the query to fetch only for the city attribute.
  • Execute NSArray * uniqueCities = [fetchedDealers valueForKeyPath:@"@distinctUnionOfObjects.city"];
+5
source

A quick way to provide a unique set of things is to use NSSet. When you have the results for a query around the city, take NSArray and do

 NSSet* uniqueResults = [NSSet setWithArray:resultsArray]; 

You can convert a collection to another collection class if it is more convenient or just an enumerator of objects to do something with all of them. I don't know if this approach or the valueForKeyPath method from Dave DeLong is effective.

0
source

Source: https://habr.com/ru/post/1304807/


All Articles