斯威夫特& Firebase-isPersistenceEnabled之后不同步 [英] Swift & Firebase - Out of sync after isPersistenceEnabled

查看:99
本文介绍了斯威夫特& Firebase-isPersistenceEnabled之后不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序与Firebase数据库之间的连接出现问题.添加后:

I have a problem with my connection between my app and firebase database. After adding:

Database.database().isPersistenceEnabled = true

对于我的AppDelegate,某些数据不同步. 要获取我的数据,我使用:

To my AppDelegate, some of the data is out of sync. To get my data i use:

self.ref?.child("Stores").observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            let store = Store()
            store.Latitude = dictionary["Latitude"]?.doubleValue
            store.Longitude = dictionary["Longitude"]?.doubleValue
            store.Store = dictionary["Store"] as? String
            store.Status = dictionary["Status"] as? String
            stores.append(store)

            DispatchQueue.main.async {
                self.performSegue(withIdentifier: "LogInToMain", sender: nil)
            }
        }
    })

然后在下一个ViewController上,我使用日期在地图上进行注释.像这样:

And on the next ViewController, i use the date to make annotations on a map. Like this:

func createAnnotation(Latitude:Double, Longitude:Double, Store:String, Status:String) {
    let annotation = CustomPointAnnotation()
    let latitude: CLLocationDegrees = Latitude
    let longitude: CLLocationDegrees = Longitude
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    annotation.title = Store
    annotation.imageName = "\(Status).png"
    map.addAnnotation(annotation)
}

但是问题是,注释的数据不再随数据库一起更改.需要先打开该应用程序,然后将其关闭,然后再次打开,然后才能正确显示数据.谁能解决这个问题?

But the problem is, that the data to the annotation dont change with the database anymore. The app needs to be opened, then closed, then opened again before the data is shown correctly. Can anyone help with that problem?

推荐答案

由于您正在使用:

Database.database().isPersistenceEnabled = true

然后您需要使用keepSynced(true),以便数据保持同步.

then you need to use keepSynced(true) so the data will stay synchronized.

持久行为:

通过启用持久性,即使用户或操作系统重新启动应用程序,Firebase Realtime Database客户端在联机时将同步的任何数据也将持久保存到磁盘,并且可以脱机使用.这意味着您的应用程序将通过使用存储在缓存中的本地数据来像联机运行一样工作.侦听器回调将继续触发本地更新.

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache. Listener callbacks will continue to fire for local updates.

keepSynced(true):

Firebase实时数据库为活动侦听器同步并存储数据的本地副本.此外,您可以使特定位置保持同步.

The Firebase Realtime Database synchronizes and stores a local copy of the data for active listeners. In addition, you can keep specific locations in sync.

有关更多信息,请检查以下内容: https://firebase.google.com /docs/database/ios/offline-capabilities

for more info check this: https://firebase.google.com/docs/database/ios/offline-capabilities

这篇关于斯威夫特& Firebase-isPersistenceEnabled之后不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆