0

I'm trying to get the top 4 scores using a scoring system with the new Firebase queries in Swift. My database looks like this:

gameScores
     $gameType
          $userId: 3
          $userId: 2
          $userId: 4
          $userId: 1
          $userId: 5
          $userId: 8
          $userId: 2
     $gameType
          $userId: 1

The integer is the score for that userId. I want to get the 4 highest scores for a given gametype. Ties don't really matter, the important part is ordering them then pulling these first 4. The integer is the score for that userId. I tried using a query which looks like this:

DataService.ds.REF_GAMESCORES.child(self.gameType).queryOrderedByValue().
queryLimitedToFirst(4).observeEventType(.Value, withBlock: {(snapshot) in

but when I print the snapshot it puts them in the same order they are in the database. I have been working with the observers for a while but this is my first time working with Firebase queries.

AL.
  • 36,815
  • 10
  • 142
  • 281
Wayne Filkins
  • 494
  • 6
  • 20
  • Possible duplicate of [Firebase queryOrderedByChild() method not giving sorted data](http://stackoverflow.com/questions/39222128/firebase-queryorderedbychild-method-not-giving-sorted-data) – Willjay Sep 08 '16 at 02:10
  • No it's not duplicate because he's ordering his by a score value but I'm ordering mine by a unique Id. So if he puts queryOrderedByChild("score") then what do I put? I don't know the id and they are all different so I can't put them in there. – Wayne Filkins Sep 08 '16 at 02:36
  • You will need to sort the data on the client – Капылов Данил Sep 08 '16 at 06:42
  • Is it returning the correct 4 just unordered? Or all of them unordered? – Mathew Berg Sep 08 '16 at 13:44

0 Answers0