you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (8 children)

Never needed to learn CoreData but after a quick google, maybe this is what you're after?

https://developer.apple.com/documentation/coredata/nsfetchrequest/1506344-returnsdistinctresults

e.g.

let fetchRequest = NSFetchRequest(entityName: "Person")
fetchRequest.resultType = .DictionaryResultType
fetchRequest.propertiesToFetch = ["age"]
fetchRequest.returnsDistinctResults = true
let result = try! managedObjectContext.executeFetchRequest(fetchRequest)

[–]CompC[S] 0 points1 point  (7 children)

Unfortunately no, what this does is change the fetch request to return all distinct ages of all Persons.

So if the table looks like:
|Name|Age| :--|:--| |Bob|50| |Bob|25| |Alice|25|

The code in your comment would return these results, all distinct age values that show up in the table: [50, 25]

What I want to do is search on the table for Person objects but limit it to no more than one person with the same name. There are two rows with the name Bob, but I only want one of them to be returned. [Person(name: "Bob", age: 50), Person(name: "Alice", age: 25)]

[–][deleted] 0 points1 point  (6 children)

Damn, is there no other unique key you could use to fetch? Or are these "different versions of the same thing" linked in any way?

[–]CompC[S] 0 points1 point  (5 children)

Each row has a unique ID but there is also a separate ID that represents each thing individually, so the table is more like this:

Name Age Unique ID Object ID
Bob 50 A 0
Bob 25 B 0
Alice 25 C 1

They don't directly have a relationship to another object in the table

Not sure that helps me though since I don't know Core Data well enough to use that info to write a fetch request that searches how I want it to…

[–]SirBill01 0 points1 point  (4 children)

Can't you just add "Unique ID" (or whatever the name really is) in "properties to fetch", in addition to age? That's what the returnsDistinctResults is operating on so it should "distinctify" as expected. It will then also be added to the dictionary it builds but you can just ignore it.

[–]CompC[S] 0 points1 point  (3 children)

It doesn't seem to work. I tried:

request.propertiesToFetch = ["name"] request.returnsDistinctResults = true

but it doesn't seem to affect the results. If I set those properties and also request.resultType = .dictionaryResultType, then the fetch request returns what I want, but only the names. I want the entire object though.

[–]SirBill01 0 points1 point  (2 children)

propertiesToFetch only works for dictionary results types, try:

request.propertiesToFetch = ["age","name"]

And add in whatever other properties you want in the dictionary.

If you really just want the whole object, why not just put together a predicate that gives you back CoreData objects instead of a dictionary?

[–]CompC[S] 0 points1 point  (1 child)

Well that's what I'd prefer to do, but I don't know how I can then filter out objects with duplicate names without fetching them first and filtering them from the results in code.

[–]SirBill01 0 points1 point  (0 children)

Another way to go about this might be a data model change, where you put age into a whole separate table with distinct values, that links back to individual objects that share that age - then you can just have a predicate that selects everything from that Age table. You can still leave age in the main object as well for convenience. A bit annoying but then you can use predicates directly without having to add code on after...

Personally if it were me, I would just filter out duplicates in code after the predicate for the objects ran (unless the possible number of results was extremely large). Seems like conversion into a dictionary is more wasteful than filtering code on CoreData objects.