0

I am creating a tableView that will populate all nearby Users within 100km - then order by their HighScore.

The code that I am using is below...

let geoFireUser = GeoFire(firebaseRef: DBProvider.instance.gameHighScoreRef)
self.circleQuery = geoFireUser?.query(at: currentUserCLLocation, withRadius: 100)

circleQuery?.observe(.keyEntered, with: { (key, location) in

    let queryRef = DBProvider.instance.gameHighScoreRef.queryOrdered(byChild: "highScore").queryLimited(toLast: 100)
    queryRef.observe(.childAdded, with: { (snapshot) in

        let userNearbyData = UserData(snapshot: snapshot)
        self.listOfNearbyUsers.append(userNearbyData)
        self.listOfNearbyUsers.sort(by: {$0.highScore > $1.highScore})

        })
    }) 
}

I have created a segmented control that will show, GLOBAL LIST and NEARBY LIST (100km).

Currently in my gameHighScoreRef - there are only 3 unique Keys (3 users). Also my Firebase structure looks like Root -> gameHighScore -> GeoFire, HighScore, UserName..

My GLOBAL LIST shows 3 users. My NEARBY LIST shows 9 users. It should only show 3 users.

To make sure that my class was working correctly, I then removed the geoFire CircleQuery and it showed 3 USERS.

How can I tweak my code to search for users within 100km. Then sort by their HighScore?

Any help would be greatly appreciated.

M.Strachan
  • 135
  • 2
  • 13
  • GeoFire already does some magic to allow it to filter by latitude and longitude. There is no easy way to also order by highscore. See http://stackoverflow.com/questions/39953512/firebase-geofire-most-popular-item-at-location/39958821#39958821 – Frank van Puffelen Jan 27 '17 at 20:56
  • Thanks for getting back Frank. Cheers – M.Strachan Jan 27 '17 at 21:24
  • @FrankvanPuffelen Can't you play some games with the geofire keys and combine the high score and uid? so the keys would be score9_uid_10 and score2_uid_30, and score4_uid_05. They would naturally fall in order by their keys? Just a thought - and I may be off base as I haven't tried it. – Jay Jan 27 '17 at 22:09
  • As my linked answer says, that is theoretically possible. But you'd have to combine them in a way that allows range filtering on all 3 values, which means you'd have to find a way to weigh score vs distance. Unless I'm overlooking something of course, that is always possible. – Frank van Puffelen Jan 27 '17 at 22:14

0 Answers0