Google’s Firebase platform includes the Real-Time Database. This is a fantastic way to coordinate quickly changing data to your application. The beauty of using this mechanism is the fact that when a change is made to your data, a notification is sent to your application to update the relevant piece of data.
There are two main advantages of using this push system as opposed to polling the data. Firstly, the changes to the database are sent and displayed on your device almost immediately. The second advantage is that this system doesn’t require downloading all of the data in the list multiple times, but only the data that has changed when it changes.
Tutorial:
You can start with a test project that I have already prepared, it has all of the Firebase frameworks that you will require already set up using Cocoapods. This will save you a bit of time and enable me to focus on the more important aspects of Firebases Real-Time Database. The test project is available here:
https://github.com/rtking1993/FirebaseTest
Now we should be starting with a single view application template with Firebase frameworks set up and ready. The first thing we should do is get the outlets properly connected to our MainViewController file. We will need a UIView, UIButton, UITextField and a UITableView. Place these onto your storyboard, similarly to what I have done below.
Next, we want to connect these objects up to some outlets, which should look something like this in your MainViewController.swift file.
We are going to be displaying a list of our items that are going to be connected to the Firebase Database. So we need to have a local instance of our items. Our items are going to be a simple model. Our model will look like this.
https://gist.github.com/rtking1993/4c0bc66ab37ab125ff92b7224e909295
This model will give us the ability to instantiate itself, either using regular parameters or a DataSnapshot that we will receive from the firebase server.
Now we can head over to our MainViewController.swift file and add an array of items. We are going to use a property observer called didSet on our array. The didSet property observer will be fired whenever the items array is set, we can put the code here to reload our table view. This way our UITableView will reload every time our items array is set.
https://gist.github.com/rtking1993/aed84f4c2edffae4f2053b9ed793d509
Now that we have an array of items, we can fill our data source methods on our UITableView. If we do this as an extension of MainViewController outside of our class, we can keep the code nice and tidy. Just like the below example.
https://gist.github.com/rtking1993/69e4cce03241fc6f4f10c9f6d1c314de
Now we are going to create this class called ItemsRemote. It will allow us to remove networking code out of our MainViewController. It will have methods to post an item to the Database and get all of our items from the Database. Having this class improves the readability of the ViewController and also enables us to have a constant DatabaseReference point for our list of items, this is important so we know we are always reading and writing from the same place.
https://gist.github.com/rtking1993/7eb1c2c668bc01d80beddceb923690e2
The last thing we need to do is add the important methods into the MainViewController.
The MainViewController will start with the viewDidLoad method, so inside here, we will call the method loadItems(). Our loadItems function uses our ItemsRemote class to get the list of items from the database. It then sets the items that it retrieves into our array of items, which will then refresh the table view.
The other two methods are going to enable us to post items to the server. All we need to do is create the add IBAction which will be called whenever the add button is pressed. We can then use the text inside the UITextField to create an Item and post it to the database using our ItemsRemote class. The last helper function called textFieldFinished just dismisses the keyboard and sets the text of the UITextField to nil after we have finished posting.
https://gist.github.com/rtking1993/9d118e0a3539680c67eb77eb30184cd7
If you have followed all of the steps correctly and I haven’t missed anything out, your item list should be working.
The source code for the finished dynamic item list is available here:
https://github.com/rtking1993/FirebaseTest/tree/feature/finished