斯威夫特& Firebase - isPersistenceEnabled后不同步

时间:2018-01-09 09:52:49

标签: ios swift firebase firebase-realtime-database data-persistence

我的应用和firebase数据库之间的连接存在问题。添加后:

Database.database().isPersistenceEnabled = true

对于我的AppDelegate,一些数据不同步。 要使用我的数据:

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上,我使用日期在地图上进行注释。像这样:

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

但问题是,注释的数据不再随数据库而改变。应用程序需要打开,然后关闭,然后再次打开,然后才能正确显示数据。任何人都可以帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

因为您正在使用:

Database.database().isPersistenceEnabled = true

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

持久性行为:

  

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

<强> keepSynced(真):

  

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

了解更多信息,请查看:https://firebase.google.com/docs/database/ios/offline-capabilities