Firebase and iOS

When it comes down to it, mobile apps are all about data. Displaying data, manipulating data, adding data, and so on. For the iOS Bike Kollective app, that data involves users and bikes. In crafting our project plan, our group decided to store this data in a cloud utilizing Google’s Firebase.

As I am implementing the List View for the app this week, I will need to find a way to retrieve and display bike data using a Table View. That means understanding how Firebase and Swift can work together.

Getting Firebase Up and Running

Once the Firebase NoSQL database is set up, the proper Firebase SDKs will need to be in place in order for our app can interact with the database. That means installing the proper Swift Package Dependencies, such as the Firebase iOS SDK via Cocoapods.

Once that is complete, we will need to ensure Firebase is imported into the relevant Swift files, in particular, AppDelegate.swift. Then, an instance of FirebaseApp will need to be declared and configured using the following method in AppDelegate.swift:

FirebaseApp.configure() 

While Firebase is set up, our database will be running as a Cloud Firestore database. To get this set up, I will follow the various steps as outlined by the Firebase documentation.

To sum up, I’ll need to add Cloud Firestore as a pod to the Podfile and then add the following line to AppDelegate.swift:

let db = Firestore.firestore() 

Retrieving Firebase Data

With Firebase and the Cloud Firestore database properly connected to the app, I will need to code the means of retrieving bike data from the database.

Luckily, Google has provided a code example on Github providing a general outline of how this process may work here. I’d first need to declare an instance of the database as so:

var db: Firestore!

And then adding the following line in the viewDidLoad() function:

db = Firestore.firestore()

Firebase refers to tables as Collections and entries as Documents. For the bike data, I would then need to access the Bike Collection to display individual Bike Documents.

This means running a method called getDocuments() on the collection and then iterating through it to apply the relevant data from each Bike Document to each Table View Cell as such:

db.collection("bikes").getDocuments() {(snapshot, err) in 
    if let err = err {
        print("Error getting documents: \(err)")
    } else {
        for document in snapshot!.documents {
            // Add data to objects in Table View Cells
        }
    }
}

As we can see, this also provides the iteration needed to apply the retrieved data to each individual Table View Cell.

Wrapping Up

With the above in mind, I can now work on simply implementing the above code into the relevant Swift files. While I will need to figure out the specifics of inserting the data into the various objects on the storyboard, I can at least rest easy that the Firebase data can be retrieved.

Leave a comment

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