Firestore and SwiftUI?! Let’s do it!

Adam Nowland
3 min readMay 10, 2021

For my final project while attending Flatiron School’s coding bootcamp, I decided to do a Swift/SwiftUI project. We don’t learn either of these in our curriculum and I was interested in trying my hand at iOS development.

I also decided to try utilizing Firebase Firestore for my backend. So, I took the plunge and started reading and watching youtube videos. One of the first hurdles I ran into, was that there wasn’t a ton of great information out there when dealing with SwiftUI. When I went to connect my Firestore to my front end, once again I hit a little bit of a wall when looking for information. So, let’s get started!!

I am going to assume that you have already started up working on your SwiftUI application, and we will fast forward to when we are going to connect to Firestore.

  1. Installation — There is two routes to go about installing Firebase; either CocoaPods or the Swift Package Manager. I chose the latter.

Once there, ctrl-c ctrl-v — https://github.com/firebase/firebase-ios-sdk.git

Hit next, then choose the most up to date version and next again! You will then be faced with all of the packages that comes with Firebase. For Firestore select Firebase Firestore and Firebase Firestore Swift — Beta.

2) Let’s move to the Firebase website for a second now. Once you have signed up for an account, go ahead and start a new project. You will follow some instructions and be asked for your bundle identifier.

Found here!

After you have your project set up you will need to configure firebase on startup!

3) This is where it’s a bit different in SwiftUI now vs older versions. In your XXXXxapp.swift file insert this code inside of the struct:

init(){FirebaseApp.configure()}

So, let’s recap where we are at. We have installed firebase and Firestore to our application, set up Firestore on the backend, and now we just need to talk about getting data from Firestore to your app. For this, there are a lot of great examples already out there. I will give you a little snippet of my code block for a reference and we can go over the meat below!

import Foundationimport Firebaseimport FirebaseAuthimport FirebaseFirestoreimport FirebaseFirestoreSwiftclass measurementRepository: ObservableObject {let path = "Measurements"let db = Firestore.firestore()@Published var measurements = [glucoseMeasurment]()init(){loadData()}func loadData(){let userID = Auth.auth().currentUser?.uiddb.collection(path).order(by: "date", descending: true).whereField("userID", isEqualTo: userID).addSnapshotListener{ (snapshot, error) inif let error = error {print(error)return}self.measurements =  snapshot?.documents.compactMap{ document intry? document.data(as: glucoseMeasurment.self)} ?? []}}

This is set up with a repository model in mind. Note the imports, I am using Firebase Anonymous Auth as well with mine, which is another great feature from Firebase. Note I bolded the important parts :) Lets access our database with Firestore.firestore(). Next we ‘listen’ that data with a snapshot listener. I refer you here to read a bit more about that. If we do not encounter an error you can then map over your data! And just like that you have a connection to your backend!

I would also strongly encourage you to watch the video below which comes directly from firebase. With the information I posted above, plus their wonderful explanation, you will have a Firestore database up and running with your app in no time!

Good luck!!!

--

--

Adam Nowland

Former foot and ankle physician. Current bootcamp student. All the time technology nerd.