How do I get data from Firebase

Goals of the blog

After reading this blog, we will be able to understand the process of getting data from Firebase. This process involves installing the Firebase SDK and implementing code to pull data from Firebase.

The requirements:

We need to write quick code to get the data from Firebase. To do this, we must first create the Firebase project. Follow the steps below to create the Firebase project:

1. Go to

2. Then you need to create a Firebase project by clicking "Add project"Button

3. You will need the GoogleService-Info.plist file for iOS SDK as this file contains the Firebase project details that will be used by the Firebase SDK

4. Then insert the plist file into the project

5. Add the following code before the return statement in your application(_: didFinishLaunchingWithOptions 🙂 in the AppDelegate.swift

FirebaseApp.configure ()

6. Then go to "Database"Option from the left menu.

7. Choose "Real-time database"and this is the database for the Firebase project. All data that is stored in or accessed from this directory comes from this directory.

8. The Firebase setup is complete. Now you have to download the Firebase SDK in no time. To do this, you need to install the following pods.

  1. Firebase / Core pod
  2. Firebase / Database pod

Suppose you have the following data in the specified structure in the Firebase database:

// The root of the tree {// Food "Food": {// Food / Milk "Milk": {// Food / Milk / Name "Name": "Milk", / / ​​grocery-items / milk / addedByUser " addedByUser ":" David "}," pizza ": {" name ":" Pizza "," addedByUser ":" Alice "},}}

After successfully installing the pods, we can move on to the coding part.

Code implementation

All data points / nodes are used to access data from the Firebase database. We create references to the nodes and then get data from that node. In other words, a Firebase reference points to a location in Firebase where data is stored. First we need to create a reference to the root node of the database structure -

let ref = Database.database (). reference (withPath: "Food")

or the following route can be used -

let rootRef = Database.database (). reference () let ref = rootRef.child ("Food")

The reference to the child node "milk" can be created with the following code:

let milkRef = ref.child ("milk")

The asynchronous listener can be appended to a reference to fetch data from Firebase observe (_: with 🙂 Method. The following code gets the data and monitors data changes in the database. Whenever changes are made to the database, the new snapshot of the node / reference is obtained and the updated data is obtained whenever changes are made to the database.

ref.observe (.value, with: {snapshot in // This is the snapshot of the data in the Firebase database // To get the value from the snapshot, we use snapshot.value print (snapshot.value as Any )})

The snapshot values ​​can then be analyzed and used in the app. A big advantage of using it observe Method over observSingleEvent The method is that whenever a value in the database is changed, you don't have to call the function again and again when the updated data is fetched from the database.


Now you should be able to use the Firebase SDK to pull the data from the Firebase database in no time.