Tutorial details

Swifting with BaasBox - Part 3 | Building a quiz app with BaasBox | iOS Tutorial

Part 3 | Building a quiz app with BaasBox

Description

Comments (4)

Overview PAGE TOP

In the first part of this series you have learned how to use the BaasBox SDK in Swift. In the second part you have learned how to fetch questions from the backend and run the game.

In this third part we are going to learn how to save the score of each game on the backend and how to load the ranking.

Saving the Score PAGE TOP

In the current version of the application we already have all the game logic in place. The "play" function detects when a round is over and dismisses the view controller. Before doing that, we need to update the score of the user on the backend. This is pretty easy to do. Here is the outline of what we need to do:

This returns a fully parsed instance of BAAUser representing the logged in user. Each user in BaasBox has four dictionaries with different visibilities. The one we are interested in is "visibleByRegisteredUsers", which means visible by all the users that have an account on the platform. We are going to create/update the property named "score" within that dictionary as follows:

This code creates the property if it does not exist (some user might have an account but have never played) or updates its current value. Once we are done we can call updateWithCompletion which will save the updates user on the backend. Summing up here is the full new function play()<br>

Building the Score View Controller PAGE TOP

The Score View Controller will load user profiles sorted by score, from the highest to the lowest, and display them in a table view. Let's see how easy it is. Create a new class named

ScoreTableViewController as a subclass of UITableViewController. Now drop a Table View Controller in the Storyboard and set its class to ScoreTableViewController. Next select the table view cell, set "scoreCellID" as cell reuse identifier and set its style to subtitle. Now open ScoreTableViewController.swift and make the following changes:

The last step is to perform the fetch request to the server. The query will retrieve the list of registered users sorted by the value of the score property. This is as easy as injecting the following dictionary in the parameters of the request.

let parameters = ["orderBy" : "visibleByRegisteredUsers.score DESC"]

The key "orderBy" is required by the server and the value specifies the property of the BAAUser class that we want to use as a criterion for sorting. We will perform the call in viewDidLoad() so change it as follows:

In case the call is successful we cast and cache the result in the array, otherwise we print the error. Finally, we need a way to show the newly implemented view controller. Open the Storyboard and add a new button named "Scores" to the HomeViewController. Control-drag from the navigation controller to ScoreTableViewController and select "show" as a segue type. Then select the segue and name it "showScores". Finally hook up the "Scores" button to an IBAction defined as follows:

We are done! Run the application and tap the scores button. It will trigger a request to fetch registered users sorted by score and show it in the table view.

Conclusion PAGE TOP

In this third part of the series you have learned how to create the view to display the ranking of users in the game. You have seen how it is easy to create a table view controller in Swift and to perform a request with parameters to the BaasBox backend. In the fourth part we will wrap up the key points of the tutorial and provide you a link with the complete application, graphics included!

Confirm vote

Please check your content

It appears as though you entered a website, phone number, or email address.
Please, do not exchange your Skype/email contact on the platform, for any questions regarding the product do
not hesitate to make your demand here, your question could be useful for future buyers on the platform.
Moreover, writing on the comments area allows us to have a record of the issues and to act in case of
any problem.