1

Say i have a Parse app (Parse.com) with a class called "Employees" with a column for a string "name" - i want to display a UITable list of the employees names. <--This part is easy.

However i do not want any repeating names. I have 25 "Bob" names and 35 "Anne" names in my list and the rest are all unique. How would i count how many times "Bob" or "Anne" appears and then only display them once in the table view?

I have been having a look around the internets and for the life of me i cannot find an answer to this problem so far - i may be too tired or spent too long coding so any help would be appreciated.

If the answer is obvious or in another question or documentation feel free to link me there.

Thanks again.

jkd359
  • 151
  • 4
  • 16

2 Answers2

1

I'm gonna assume you are talking about parse.com

PFQuery* query = [PFQuery queryWithClassName:@"Employees"];
NSArray* reqQuery = [query findObjects];
NSArray* uniqueNames = [reqQuery valueForKeyPath:@"@distinctUnionOfObjects.name"];

Sadly, parse doesn't have that feature built-in yet afaik, so you have to do it on the client.

Andrei
  • 153
  • 5
  • Saying what could work using another technology (non-parse) which is not available is not an answer to the question. – Marius Waldal Mar 11 '14 at 11:40
  • PFQuery is actually a class from the Parse for iOS framework. http://parse.com/docs/ios/api/Classes/PFQuery.html – Andrei Mar 11 '14 at 14:56
  • Sorry, I misunderstood your last comment. Your suggestions is perfectly viable. – Marius Waldal Mar 11 '14 at 20:45
  • Good answer, however i think i am misinterpreting something. So i perform my query - first two lines, done. Then i think i am not understanding the @"distinctUnionOfObjects.name" part, i am sure you have explained it well enough it is just me. So what does this "distinctUnion" represent? – jkd359 Mar 12 '14 at 05:35
  • It works fine for me, i created a table "Employees", with a column "name". This is the description of reqQuery: <__NSArrayM 0xa963730>( { name = John; }, { name = Bob; }, { name = Anne; }, { name = Bob; } ) And this is the uniqueNames array: <__NSArrayI 0xab72cb0>( Bob, Anne, John ) – Andrei Mar 12 '14 at 08:39
  • Ah, it has "clicked" now. Just for anyone else who looks at this: Once you have this array of @"distinctUnionOfObjects.Anne", you then can just search through that array for repeated values using NSCountedSet or any other method you see fit for filtering through your array. Thanks again Andrei! – jkd359 Mar 12 '14 at 08:42
  • 1
    You might want to read more about it here: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html . "@distinctUnionOfObjects.name" uses "name" because that would be the name of the column in Parse that holds the name strings. And uniqueNames is the array with unique values (duplicates removed). – Andrei Mar 12 '14 at 08:45
0

Parse.com does not support distinct queries (which is quite common with NoSQL databases). You would need to query for all Employees, and get the distinct values yourself. An easy way of doing this is to create an NSSet from your array of usernames:

NSSet *empSet = [[NSSet alloc] init];
[empSet setByAddingObjectsFromArray:usernameArray];
Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • Since he is using Parse, he probably just gets the names from the array of PFObjects. So he would have to first build the array with all the names, then get the set with the unique values. I think the "distinctUnionOfObjects" way is better for this. – Andrei Mar 11 '14 at 15:00