Swift和Firebase – isPersistenceEnabled后不同步

我的app和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) } 

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

由于您使用:

 Database.database().isPersistenceEnabled = true 

然后你需要使用keepSynced(true)这样数据就会保持同步。

持久性行为:

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

keepSynced(真):

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

有关详细信息,请访问: https : //firebase.google.com/docs/database/ios/offline-capabilities