Creating a Firebase Data Model

If you are writing an app that interacts with a Firebase database, there are a lot of times where you would like to just put some data into the database and not deal with the Firebase technicalities. Today we will build a Firebase Data Model class that will allow you to easily interact with your Firebase Database!

You can take a look at the full project on my Github.

If you are not familiar with getting started with Firebase or using Table Views I have tutorials on both!

Establishing a Firebase Reference in Swift

Creating a simple Table View in Swift

What you will learn in this tutorial

  1. Adding to a list in a Firebase Database
  2. Removing an item from a list
  3. Creating Observers to handle data events

FirebaseDataModel.swift

The goal of this tutorial is to create a swift class that interacts with the realtime database. It will have the following implementations

  1. init() – Where the configuration for the database occurs
  2. addItem() – Where we will be able to add an item to the Firebase database
  3. removeItem() – Where we will be able to remove an item
  4. setObservers() – Where we will set observers to handle data change events

init()

In the init() of our FirebaseDataModel class we need to do a little configuration to ensure that everything is properly set up to interact with the Firebase Database.

  1. FIRApp.configure() must always be called before doing anything “Firebase” in your app.
  2. Set our Firebase reference property
  3. Call the setObservers() method. (Don’t worry about the this method for right now, we’ll get there!)

screen-shot-2016-11-11-at-12-39-28-pm

The Data Structure

This is how the data is structured in my Firebase Realtime Database. Firebase structures its data in a JSON format so every “node” in the database is a key/value pair.

Screen Shot 2016-11-11 at 12.20.07 PM.png

The database contains a data node which contains a list node that has child items. Each of the items in the list has a generated key.

Adding data to Firebase

We are going to maintain two copies of our data at all times. One will be the online Firebase Database and the other will be a local Swift data structure. One of the coolest features about Firebase is that it will maintain an offline version of it’s database for the app to use in the case that internet connection is not available!

The references to our two databases will be a Swift String array and a Firebase reference object.

Screen Shot 2016-11-11 at 12.35.00 PM.png

It is very simple to add data in Swift. You simply need to obtain a reference to the path where you would like to add the data and call the setValue() method. For this app, I would like to add items to the list in the path “data/list”. Since Firebase makes all nodes key value pairs, one easy way to make a list of items is to call the childByAutoId() method which will create a new child node with an automatic id as the key and our list item as the value.

Screen Shot 2016-11-11 at 12.33.55 PM.png

Now anytime you call the addItem() method within the app it will  add the data to Firebase!

Removing Data

Removing data from the Firebase database takes a little more work than adding. We will write a removeItem() method that is similar to the addItem() method we just wrote.

This is what our removeItem() method will look like.

Screen Shot 2016-11-11 at 2.19.47 PM.pngEach step number matches to the number in the Swift comment

  1. First we remove the item from our local database
  2. The next step is to call the observeSingleEvent() method on the Firebase reference to the list node. What this essentially does is give us a snapshot object of type FIRDataSnapshot that we can use to interact with the database! The observerSingleEvent() method requires a FIRDataEventType value signifying what type of event you would like to observe and a closure block that contains the code to execute during the event.
  3. We then iterate through the list children until we find the one that contains the item to remove.
  4. Once the item to remove is found in the snapshot’s children, we call removeValue() on the reference to that item
  5. This is a NotificationCenter post which calls the “refresh” method that refreshes the UI with the new values

I told you removing data requires more work! At least now we have a removeItem() method that we can call to easily remove things from our database! This is the beauty of Encapsulation!

Setting Observers

The last step is to set an observer! (You already saw the observeSingleEvent() method which we used to gain a reference to our Firebase data in the removeItem() method.) Setting an observer will handle an event every time it occurs. The event we will be observing is a data change event. This means that anytime data is changed in our Firebase Realtime Database,  the app will get notified and we can handle the new data!

Screen Shot 2016-11-12 at 10.37.43 AM.png

In our setObservers() method that we call in init():

  1. Create a new Firebase observation which takes in the FIRDataEventType and the block of code to run when the event occurs.
  2. We reset the local data because we will populate a new instance with the new data
  3. Iterate through the list items and add it to the local data structure
  4. Call the NotificationCenter post method to refresh our UI

Thats it! Now anytime the data is changed, the app will be updated with the new data in real time! This is much nicer than the typical way of fetching new data in an app which involves pulling down on the screen.