CoreData get distinct values of Attribute
You should use the backing store to help you get distinct records.
If you want to get an array with just John, Betty, Edward here's how you do it:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:self.managedObjectContext];
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work.
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct names, only ask for the 'name' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]];
fetchRequest.returnsDistinctResults = YES;
// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);
Swift Core Data - Request with distinct results
You need to set
request.resultType = NSFetchRequestResultType.DictionaryResultType
It returns dictionaries, but the distinct filter should work.
If you do not want to go down that route, filter in memory (also recommended). Do a normal fetch and then
let distinct = NSSet(array: results.valueForKeyPath("docID") as [String])
With Swift 2.0 I prefer
let distinct = NSSet(array: results.map { $0.docID })
NSFetchRequest: Query for distinct values and count other properties values
The correct answer to this question to refactor the data model in order to avoid redundancy.
The country strings are repeated unnecessarily in the table. Additionally, you make a simple query gratuitously complicated. The model should reflect your data, and writing out "USA" for every American city is just not smart or efficient.
Your data model should look like this
Country <----->> City
Now you can just fetch all countries and get the cities with cities.count
.
Swift 3. NSFetchRequest propertiesToFetch
I forgot to set resultType
for NSFetchRequest.
let fetchRequest = NSFetchRequest<MyClass>(entityName: "MyClass")
fetchRequest.propertiesToFetch = ["myAttributeName"]
fetchRequest.resultType = .dictionaryResultType
Related Topics
Swift Sending Data from Child View to Parent View Controller
Swift Nfc Mifare - Nfciso7816Apdu Sendmifare Command Not Supported
Do I Need to Wrap My Alamofire Calls Inside Dispatch_Async
How to Change the Nstimeinterval of an Nstimer After X Seconds
How to Share Published Model Between Two View Models in Swiftui
Segue Out of Navigation Controller
Change Push Notification Sound File, Only Works After iOS Reboot
Caching Images in Collectionviewcell in Swift
My App Is Rejected with Exception Exc_Breakpoint (Sigtrap)
Swiftui: How to Change the Tint Color (Background Color) of a Navigationview
Uiimage Aspect Fill When Using Drawinrect
iOS How to Correctly Handle Orientation When Capturing Video Using Avassetwriter
How to Store JSON Decodable Values into Coredata Using Swift
Launch Watch App into Middle View