【发布时间】:2018-06-18 07:49:53
【问题描述】:
我的应用程序和 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)
}
但问题是,注释的数据不再随数据库而改变。该应用程序需要先打开,然后关闭,然后再次打开,然后才能正确显示数据。谁能帮忙解决这个问题?
【问题讨论】:
-
我目前正在自己解决 Firebase 的问题。我想你可能会发现你的观察块在启用持久性的情况下被多次调用。第一次从本地存储返回值,第二次从服务器返回。
-
firebase.google.com/docs/database/admin/retrieve-data 对于每个现有的孩子触发一次此事件,然后每次将新的孩子添加到指定路径时再次触发。我会在下一个 VC 中使用 .childChanged 或 .value。顺便说一句@PeterHaddad 提到了observeSingleEventOfType,但根据我的经验,在大多数情况下,这绝对不会让你得到最新的结果。
标签: ios swift firebase firebase-realtime-database data-persistence