How to fetch data from Firebase in Swift Language (iOS)?

How to fetch data from Firebase in Swift Language (iOS)?

Objectives of the blog

How to fetch data from Firebase in Swift Language (iOS)?

After reading this blog we will be able to understand the process to fetch data from Firebase. This process includes installation of Firebase SDK and implementation of code to fetch data from Firebase.

Pre-requisites

We need to write swift code to fetch the data from Firebase. For this first we will have to create the Firebase project. Follow the steps given below to create the Firebase project –

1. Go to https://console.firebase.google.com

2. Then you will have to create Firebase project by clicking on “Add Project” button

 

add-project: How to fetch data from Firebase in Swift Language (iOS)?

3. You need GoogleService-Info.plist file for iOS SDK as this file contains the details of the Firebase project which are used by Firebase SDK

4. Then add the plist file in the project

5. Add the following code before the return statement within application(_:didFinishLaunchingWithOptions:) in AppDelegate.swift file

FirebaseApp.configure()

6. Then go to “Database” option from the left menu.

 

goto-database: How to fetch data from Firebase in Swift Language (iOS)?

7. Select “Realtime Database” and that is the database for the Firebase project. All the data that is stored in or fetched from is from this location.

8. Firebase setup is completed

Now you will have to download Firebase SDK in the swift. For this you will have to install the following pods.

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

Let suppose you have the following data in the given structure in the Firebase database –

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

 

After successful installation of the pods, we can move on to the coding part.

Code Implementation

How to fetch data from Firebase in Swift Language (iOS)?

All the data points/nodes are used to access data from Firebase database. We create references to the nodes and then fetch data from that node.

In other words a Firebase reference points to a location in Firebase where data is stored.

First we will have to create reference to the root node of the database structure –

let ref = Database.database().reference(withPath: "grocery-items")

or the following way can be used –

 

let rootRef = Database.database().reference()
let ref = rootRef.child("grocery-items")

Reference to the child node which is “milk” can be created using the following code –

let milkRef = ref.child("milk")

Asynchronous listener can be attached to a reference in order to retrieve data from Firebase using observe(_:with:) method.

The following code will fetch the data and keep listening to the data changes on the database. As soon as any change is made in the database, the new snapshot of the node/reference is fetched and we get the updated data as soon as any change is made in the database.

 

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

Then the snapshot values can be parsed and used in the app.

One big advantage of using observe method over observeSingleEvent method is that you don’t have to call the function again and again as the updated data is fetched from the database as soon as any value is changed in the database.

Summary

Now you should be able to fetch the data from the Firebase database in swift using Firebase SDK.

Please feel free to get in touch with us at support@knowband.com for any issues.

We also provide No Code Mobile App solutions for Prestashop, Opencart, Magento 2 and Woocommerce. Know more here:

 

Leave a Reply

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