Seng Wee
Seng Wee

Reputation: 624

Displaying Leaderboard using UITableViewController with Firebase backend (Swift 4.1)

I am using Firebase to store my backend data. I am trying to display a leaderboard using a UITableViewController in Swift.

One of the data source methods include,

override func tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

which in my case requires me to fetch data from Firebase and store the data in a local array variable e.g var leaderboardArr = [Int]()

Problem arises when there is a change to the rankings in the leaderboard. I know Firebase has built-in methods to sort the data and adjust the rank accordingly but how do I manage the local array variable?

UITableViewController dataSource method displays the data according to the indices of the local array variable.

How to manipulate the array according to the changes in data from Firebase??

Upvotes: 0

Views: 269

Answers (1)

Nick
Nick

Reputation: 502

When the user refreshes (or when you decide the view should refresh) retrieve the leaderboard stats from Firebase again and then reload the table view.

If you're looking to update the data in real time you can add a ChildEventListener. So when the array changes, it activates the listener, and from there you can retrieve the necessary data and reload the view.

Here is some reference: https://firebase.google.com/docs/reference/android/com/google/firebase/database/ChildEventListener

Upvotes: 2

Related Questions