有关有限查询参考上的keepSynced(true)的一些问题 [英] Some questions about keepSynced(true) on a limited query reference

查看:39
本文介绍了有关有限查询参考上的keepSynced(true)的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下数据库结构的Firebase支持的应用程序:

I have an Firebase-backed app with the following database structure:

posts
      /uid
           /postId

最初,我将使用 ObserveEventOfType .childAdded posts/uid 节点加载数据.这将同时为我的应用程序的所有用户频繁加载陈旧数据(每天约5次).当尝试通过发布新帖子来更新数据时,Firebase仍会返回陈旧数据.

Originally, i'd load data from the posts/uid node using ObserveEventOfType with .childAdded. This would load stale data frequently (~5 times a day) for all users of my app simutaneously. When attempting to update the data by making a new post, Firebase would still return stale data.

结果,我决定尝试 keepSynced .现在,如果我的引用看起来像这样:

As a result, I decided to try keepSynced. Now, if my reference looked like this:

reference = Database().database.reference.child("posts").child(uid)

keepSynced 将在该节点上加载所有数据,如果该节点上有很多子节点,则下载量可能非常大.因此,我决定将引用/查询更改为:

keepSynced would load all of the data at that node, which could amount to very large downloads if there are many children in that node. So, I decided to change the reference/query to:

reference = Database().database.reference.child("posts").child(uid).queryLimited(toLast: 25)

为此节点打开 keepSynced 时,它将成功同步节点中的最后25个子节点.但是,我仍然面临着相当频繁地接收过时数据的问题.所以这是我的问题:

When turning keepSynced on for this node, it syncs for the last 25 children in the node successfully. However, I still am facing the issue of receiving stale data rather frequently. So here are my questions:

  1. 在受限查询上添加 keepSynced 模式时,它是否仅从添加了它的初始节点同步,还是始终仅同步该节点下的25个最新子节点?

  1. When adding the keepSynced mode on the limited query, does it only sync from the initial node you added it to, or does it always just sync the 25 latest children under that node?

在代码中添加 keepSynced(true)行的最佳位置在哪里?在加载引用之前,是在 viewWillAppear 中还是在实际的下载回调中?

Where is the best place to add the keepSynced(true) line in code? Before we load the reference, in viewWillAppear, or inside of the actual download callback?

类似地,哪里是使用 keepSynced(false)的最佳位置?

Similarly, where is the best place to use keepSynced(false)?

当应用淡入背景时, keepSynced 侦听器是否删除?

Do the keepSynced listeners delete when the app fades into the background?

为什么 keepSynced 有时无法解决子更新?

Why does keepSynced sometimes not address for child updates?

我目前在用于加载帖子的函数中使用 keepSynced(true),该函数在 viewDidLoad 上调用.

I currently use keepSynced(true) inside of the function I use to load posts which is called on viewDidLoad.

谢谢.

推荐答案

顾名思义, keepSynced(true)可以使您调用的所有查询或引用在本地缓存中保持同步.从字面上看,它只是将空观察者附加到该查询/引用.因此,在您的 Database().database.reference.child("posts").child(uid).queryLimited(toLast:25)中,它将同步最后25个子节点,并继续同步这些(删除以前的,再添加新的).

As its name implies keepSynced(true) keeps whatever query or reference you call it on synchronized in the local cache. It quite literally just attaches an empty observer to that query/reference. So in your Database().database.reference.child("posts").child(uid).queryLimited(toLast: 25) it will sync the last 25 child nodes, and keep synchronizing those (removing previous ones as new ones are added).

如果您反复侦听完全相同的数据,则Firebase Realtime Database缓存机制最可靠地工作.具体来说,连接到 Database().database.reference.child("posts").child(uid) .value 侦听器可能看不到通过 Database().database.reference.child("posts").child(uid).queryLimited(toLast:25).这是因为Firebase客户端保证永远不会触发部分更新的事件,并且在此示例中,它不能保证它具有第一个引用中的所有数据.

Firebase Realtime Database caching mechanism works most reliably if you repeatedly listen for the exact same data. Specifically, a .value listener attached to Database().database.reference.child("posts").child(uid) may not see data that was cached through Database().database.reference.child("posts").child(uid).queryLimited(toLast: 25). This is because the Firebase client guarantees to never fire events for partial updates, and in this example it can't guarantee that it has all data from the first reference.

对于您的问题:

  1. 请参阅上文...

  1. See above...

最常见的是将它们添加到 viewWillAppear 中.

It's most common to add them in viewWillAppear.

我不确定您为什么要称呼 keepSynced 为false,所以在那里不推荐任何东西.

I'm not sure why you'd want to call keepSynced false, so can't recommend anything there.

不确定这是否是您的意思,但 keepSynced(true)在应用程序的运行之间不会持久存在.因此,每次您的应用/视图启动时,您都必须调用 keepSynced(true).

Not sure if this is what you mean, but keepSynced(true) is not persisted between runs of the app. So you have to call keepSynced(true) each time your app/view starts.

请参阅上文...

通常,您似乎尝试通过以不同方式调用API来解决API的工作方式.我通常不会从中看到很好的结果.如果您希望应用程序的行为不同于API的行为,请考虑创建一个自定义包装器,然后将数据缓存在其中.

In general you seem to try and work around the way the API works, by calling the API in different ways. I typically don't see great results from that. If you want your app to behave differently than the API does, consider creating a custom wrapper, and caching the data there.

这篇关于有关有限查询参考上的keepSynced(true)的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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