swift - 启用observeSingleEvent + 持久性是否可以获取最新数据?

标签 swift firebase

我正在尝试从 firebase 获取一些数据。知道启用持久性后如何获取最新数据(不是来自缓存)吗?我尝试过保持同步;我仍然得到陈旧的数据。这是正确的用法吗?

userRef = FIRDatabase.database().reference().child("<path>")
userRef.keepSynced(true)
userRef.observeSingleEvent(of: .value, with: { snapshot in
   ...stale data here...
})

或者唯一的选择是使用observe而不是observeSingleEvent?我不喜欢这样的事实:通过观察,我首先获取缓存数据,然后事件使用来自服务器的数据第二次触发。因此,通过观察,当我导航到此屏幕时,首先我看到一个空白表格,然后我看到包含过时数据的表格,然后我看到包含最新数据的表格。

谢谢。

编辑:

https://stackoverflow.com/a/34487195/1373592 - 这篇文章说 keyySynced 应该可以工作。但这对我不起作用。我想知道我是否做错了什么。

最佳答案

我检索了一些解释,我认为这可能对您的情况有所帮助:

ObserveSingleEventType with keepSycned will not work if the Firebase connection cannot be established on time. This is especially true during appLaunch or in the appDelegate where there is a delay in the Firebase connection and the cached result is given instead. It will also not work at times if persistence is enabled and observeSingleEvent might give the cached data first. In situations like these, a continuous ObserveEventType is preferred and should be used if you absolutely need fresh data.

我认为您没有选择使用连续监听器。但为了避免性能问题,当您不再删除听众时,为什么不删除自己呢?

关于swift - 启用observeSingleEvent + 持久性是否可以获取最新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47331559/

相关文章:

inheritance - 是否可以将 Swift 泛型类函数返回类型限制为同一个类或子类?

swift - 在 Swift 中运行相同代码 n 次的最短方法是什么?

ios - 尝试运行时出错 `pod trunk push [cocoapod].podspec`

ios - 更新到最新的 Swift 版本问题

android - 无法从Firebase数据库检索数据以显示在我的文本框中

android - Firebase Analytics UTM Activity 跟踪不起作用

ios - 有没有一种简单的方法可以在 Swift 中暂时禁用 Sprite 的物理体? ( SpriteKit )

javascript - 无法让 Angularjs 在渲染模板之前等待数据

javascript - 当我从 firebaseConfig.js 文件导入 firebase 时会发生什么?

rest - 如何生成 JWT token 以与 Firebase REST API 一起使用?