bradford gray bradford gray - 1 year ago 135
iOS Question

I'm trying to create a leaderboard using Firebase and show the username, highscore, region.

My goal is to create a leaderboard using Firebase and to show the username, high score and region in a tableview. My current Database looks like this enter image description here

I have everything under user at the moment. I did it this way so i could increment data for the current user. Unfortunately, by doing it this way, I don't think I can let others see someone elses high score or username.
Is there a solution to this problem using the current node setup, or do I need to change it. And if so can someone give my examples of how the database should look.

Answer Source

You should separate your user specific data from the leaderboards.

You have your users node in which your user's data under their unique user id. Then you also can create a leaderboards node and under the same unique user id, your leaderboard specific data. This way you won't have to fetch all the data at once only to know the user specific data.

The connection is the user id, that is the unique id, that your user is going to know, and retrieve his/her data by. The other players can just retrieve the leaderboards node by a query. You should also store some seemingly redundant data, for instance the username which you would both need for the user's node and for the leaderboards.

Don't forget to apply security rules, so the user can only write to his/her data.

Looking like this:

enter image description here

The authenticated user who knows his/her UID can then listen to his/her own leaderboard score by fetching data on this path: "leaderboards/UID" right.

Other players should just have to use the path: "leaderboards" to fetch every player on the leaderboard. To order them and only fetch the top 10 players you have to use queries.

Use queryOrderedByChild to order your data by highscore For this you should also add the security rule: ".indexOn": "highscore".

To fetch the top10 players use queryLimitedToFirst setting it to 10, it will only download the top 10 players.

You should read more about queries:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download