我是编程和iOS开发的新手。我正在尝试使用Firebase中的Firestore数据库制作一个应用程序。我不知道它是否正常,但是当我尝试从Firestore数据库中获取数据时,对我来说似乎太长了。我不知道我是否犯错

这是我的代码,用于从Firestore获取所有城市数据

参考:

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)
    }
}

我在getAllCitiesDataFromFirestore类中使用CityKM方法来获取存储在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
        ]
    }


}

从我的调试区域打印

“从Firestore获取所有城市数据所需的时间:1.8787678903 s。”

有可能使其更快吗? 1.8s正常吗?我在代码中是否犯了一个错误,使请求数据花费的时间太长?我希望我可以使请求时间小于一秒

我认为互联网速度不是问题,因为我可以在YouTube上打开视频而无需缓冲

最佳答案

这种表现听起来比我所看到的要差,但没有什么过分的。从云中加载数据只需要时间。隐藏该延迟的一种快速方法是利用Firebase的内置缓存。

当您调用getDocuments时,Firebase客户端需要在服务器上检查文档的值之后才能调用您的代码,然后代码会向用户显示该值。如前所述:无法加快代码读取速度,因此用户看到文档之前至少要花1.8秒的时间。

如果改为使用addSnapshotListenerlisten for realtime updates from the database,则Firebase客户端可能能够立即使用其本地缓存中的值调用您的代码,然后在服务器上的数据更新的情况下,稍后重新调用您的代码。

08-17 20:52
查看更多