iOS Tutorials: Using Real Time Database on Firebase

Overview

In this tutorial, we will be creating a table in which we will store all the information onto a database. We will be using a little of JSON which can be referred as JavaScript Object Notation. We will also be using Firebase as our database. If you have no idea what Firebase is, feel free to check out the official site. Firebase is a popular tool made by Google and is widely used by developers around the world. It would be an advantage if you know how to use Firebase. We will be taking the approach of MVC or in other word, Model, View, Controller. We will be using all Model View Controller for this project. Most project uses MVC so this is not a big deal.

Creating Database on Firebase

If you do not have an account on Firebase, please do so by going to the official site and get yourself a free account. When you have your account, come back here so we could go ahead and create our first firebase project.

You will be led to the Firebase Console where you could create your project. Click Add Project. Fill in the necessary information and go ahead and click Create Project.

Once you have completed the step, you will be led to the front screen. In our case, we will be using iOS so go ahead and click Add Firebase to your iOS App.

There are at least 5 steps that needs to be completed here. First, we are going to add our iOS Bundle ID which can be found by first creating your XCode Project – you will have to create your project on XCode or you wont be able to find your Bundle ID. Then give it a random nickname.

Once you are through Step 1, the next step requires you to download the Config file which is called GoogleService-Info.plist. Download that and add it onto your project.

The following step requires you to add Firebase SDK through Cocoapods. If you are not familiar with Cocoapods, rest assured as I have already prepare a tutorial for you on that. You can check it out here.

Once you are done installing all the requirements for Cocoapods and is familiar with Cocoapods, we will go to the next step of adding the initialization code at AppDelegate

The last step requires you to run your app to verify the installation. However in most cases, you could just Skip the step if its taking too long.

Connecting Database on XCode

Once you are done with all the above steps, lets go into our project that we have create from XCode. The next step is to define and create a reference for our database. With this, you are able to establish a connection to your Firebase database using the provided path. Here, we use a child path named to-do.

Now, lets go back to Firebase Console and configure the database by clicking on Database

Create a realtime database by clicking on Create database

It will then pop up some rules about the database being created, make sure to pick Start in test mode as that would allow you to read and write.

Creating Model

On your XCode, create a new Swift File by simply going to File > New > File > Swift File. We will be creating a Model using struct to represent data. With the code below, we are able to transmit data to the server and receive data from the server by stating our data types.

You’ll notice init in the code below, we are initializing the instances here. Meaning, I could just pass in the variable I have receive later. Snapshot basically allows me to retrieve the value of the database. By purely printing snapshot, I get to witness the JSON output.

Creating View

The view used here is TableViewController, Navigation Controller and Bar Button Item.

Creating Controller

We wont be using ViewController here. I deleted ViewController and recreated using TableViewController.

Once you have the controller up and going, we are going to create some variables. Here we import the Firebase SDK, created a new variable inheriting from the Model Classes we created earlier, and also define and create reference to the database.

Always make sure that the reference is the same with the one on your database. I named it ToDo in my case.

 

Adding Item into Firebase

Create an IBAction with the Bar Button Item that we create on the Main.storyboard. Here, we will create an AlertController that will pop up with a TextField for us to fill information. Once we click, save, it will go straight to the database.

The database for Firebase is pretty strong. Once you submit it, it appears right away.

Removing Item into Firebase

This is really straight forward to remove the item from the list. We only need to call removeValue().

By sliding, the delete button will show up and once you click it. It will remove it simultaneously from the tableview and database.

The code is uploaded on Github

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*