Firebase实时数据库观察者同步和数据流量Swift

时间:2018-12-06 11:14:20

标签: ios swift firebase firebase-realtime-database

关于从firebase数据库读取的信息,我不清楚。 当我将观察员当成孩子时,它会只是下载添加的新帖子,保留本地存储的文件,还是每次启动我的应用程序时都会下载整个分支? 在Firebase指南页面上,我读到Every client connected to a Firebase database maintains its own internal version of any active data. When data is written, it's written to this local version first.

我在读过一家初创公司的案例后,问起了一夜之间总共3万张账单,我很高兴避免自己重复。 当我的应用程序在编程的时间醒来执行检查时,如果我每次都下载整个数据库,那将是非常昂贵的。 离开应用程序时,我确实分离了听众,以便管带宽和电池友好,并且在不使用时不听新帖子。 这是所涉及功能的代码。

发红:

func displayAlerts() {

        ref = Database.database().reference()

        databaseHandle = ref?.child("Community").child("Alert Notifications").observe(.childAdded, with: { (snapshot) in

            //            defer { self.dummyFunctionToFoolFirebaseObservers() }
            guard let data = snapshot.value as? [String:String] else { return }
            guard let firebaseKey = snapshot.key as? String else { return }
            //                let date = data!["Date"]
            //                let time = data!["Time"]
            let dataLatitude = data["Latitude"]!
            let dataLongitude = data["Longitude"]!

            let type = data["Description"]!
            let id = Int(data["Id"]!)
            let doubledLatitude = Double(dataLatitude)
            let doubledLongitude = Double(dataLongitude)
            let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)

            print("Firebase alerts posts retrieved")

            //            print("Longitude Actual DataKey is \(String(describing: firebaseKey))")

            //            print("fir long \((snapshot.value!, snapshot.key))")
            let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!)

            self.userAlertNotificationArray.append(userAlertAnnotation)  // array of notifications coming from Firebase
            print("userAlertNotificationArray after retrieving from Firebase is : \(self.userAlertNotificationArray)")

            MapArray.alertNotificationCoordinatesArray.append(recombinedCoordinate) // array for checkig alerts on route
            print("alertNotificationCoordinatesArray after retrieving from Firebase is : \(MapArray.alertNotificationCoordinatesArray)")
            self.mapView.addAnnotation(userAlertAnnotation)

        })
    }

和分离:

override func viewDidDisappear(_ animated: Bool) {

    ref!.removeObserver(withHandle:databaseHandle!)
}

我应该使用removeObserverWithHandle还是removeAllObservers

0 个答案:

没有答案