Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make getting data faster from Firebase Firestore?

I am new in programming and in iOS development. I am trying to make an app using Firestore database from Firebase. I don't know if it is normal or not, but when I am trying to get a data from firestore database, it seems too long for me. I don't know if I make a mistake or not

here is my code to get all city data from firestore

reference :

import Foundation
import FirebaseFirestore
import Firebase

enum FirestoreCollectionReference {
    case users
    case events
    case cities

    private var path : String {
        switch self {
        case .users : return "users"
        case .events : return "events"
        case .cities : return "cities"
        }
    }

    func reference () -> CollectionReference {
        return Firestore.firestore().collection(path)
    }
}

I use getAllCitiesDataFromFirestore method in CityKM class to get the city data that stored in firestore

class CityKM {
    var name : String
    var coordinate : GeoPoint

    init (name: String , coordinate: GeoPoint ) {
        self.name = name
        self.coordinate = coordinate
    }

    init (dictionary: [String:Any]) {
        // this init will be used if we get data from firebase observation to construct an event object

        name = dictionary["name"] as! String
        coordinate = dictionary["coordinate"] as! GeoPoint
    }

    static func getAllCitiesDataFromFirestore (completion: @escaping ( [CityKM]? )->Void) {
        // to retrieve all cities data from Firebase database by one read only, not using realtime fetching listener

        let startTime = CFAbsoluteTimeGetCurrent() // to track time consumption of this method

        FirestoreCollectionReference.cities.reference().getDocuments { (snapshot, error) in
            if let error = error {
                print("Failed to retrieve all cities data: \(error.localizedDescription)")
            } else {
                print("Sucessfully get all cities data from firestore")

                guard let documentsSnapshot = snapshot, !documentsSnapshot.isEmpty else {
                    completion(nil)
                    return
                }

                let citiesDocuments = documentsSnapshot.documents
                var cityArray = [CityKM]()

                for document in citiesDocuments {
                    guard let cityName = document.data()["name"] as? String,
                          let cityCoordinate = document.data()["coordinate"] as? GeoPoint else {return}

                    let theCity = CityKM(name: cityName, coordinate: cityCoordinate)
                    cityArray.append(theCity)
                }

                completion(cityArray)

                let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime // to track time consumption of this method
                print("Time needed to get all cities data from Firestore : \(timeElapsed) s.") // to track time consumption of this method

            }
        }
    }


}



extension CityKM {

    // MARK: - User Helper Methods
    func toDictionary() -> [String:Any]{
        return [
            "name" : name,
            "coordinate" : coordinate
        ]
    }


}

from my debugging area, it is printed

"Time needed to get all cities data from Firestore : 1.8787678903 s."

is it possible to make it faster? Is 1.8s normal? am i make a mistake in my code that make the request data takes too long time ? I hope that I can make request time is below one second

I don't think the internet speed is the problem, since I can open video on youtube without buffering

like image 225
Agung Laksana Avatar asked Sep 01 '25 02:09

Agung Laksana


1 Answers

That performance sounds a bit worse than what I see, but nothing excessive. Loading data from the cloud simply takes time. A quick approach to hide that latency is by making use of Firebase's built-in caching.

When you call getDocuments, the Firebase client needs to check on the server what the document's value is before it can call your code, which then shows the value to the user. As said: there is no way to speed up this reading in your code, so it'll always take at least 1.8s before the user sees a document.

If instead you listen for realtime updates from the database with addSnapshotListener, the Firebase client may be able to immediately call your code with values from its local cache, and then later re-invoke your code in case there has been an update to the data on the server.

like image 186
Frank van Puffelen Avatar answered Sep 02 '25 17:09

Frank van Puffelen