为新数据添加了 Firebase onChild [英] Firebase onChildAdded for new data

查看:33
本文介绍了为新数据添加了 Firebase onChild的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含 50,000 个项目的列表存储在我的 firebase 参考中,并且自客户端上次在线并收听以来已将 5 个项目添加到该列表中,那么我必须使用哪个回调以便它仅被触发对于已添加的 5 个新项目?

If I have a list of 50,000 items stored in my firebase reference, and 5 items have been added to that list since the last time the client was online and listening, which callback would I have to use such that it is only triggered for the 5 new items that have been added?

我使用 Firebase.getDefaultConfig().setPersistenceEnabled(true); 在我的客户端上启用了离线持久性.我有一个监听器绑定到一个活动,监听添加到参考中的儿童.每次创建活动时,都会为引用中的所有数据调用 onChildAdded.是否可以仅针对本地缓存和 firebase 服务器上的数据之间的差异"调用 onChildAddedonChildRemoved?或者,如果这是不可能的,那么在 firebase 的最新更新之后只触发那些 onChild* 回调?

I have offline persistence enabled on my client with Firebase.getDefaultConfig().setPersistenceEnabled(true);. I have a listener bound to an activity listening for children added to a reference. Everytime the activity is created onChildAdded is called for all the data in the reference. Is it possible to make onChildAdded and onChildRemoved be called only for "diff" between my local cache and the data on the firebase server? Or if that's not possible, then to trigger only those onChild* callbacks after the most recent update from firebase?

推荐答案

每次创建活动时,都会为引用中的所有数据调用 onChildAdded.是否可以仅针对本地缓存与 Firebase 服务器上的数据之间的差异"调用 onChildAdded 和 onChildRemoved?

Everytime the activity is created onChildAdded is called for all the data in the reference. Is it possible to make onChildAdded and onChildRemoved be called only for "diff" between my local cache and the data on the firebase server?

不,这是不可能的.来自关于事件类型的文档:

No, this is not possible. From the documentation on event types:

child_ added 为每个现有子项触发一次,然后每次将新子项添加到指定路径时再次触发

child_added is triggered once for each existing child and then again every time a new child is added to the specified path

现在回到你最初的问题:

Now back to your initial question:

我必须使用哪个回调才能只为已添加的 5 个新项目触发?

which callback would I have to use such that it is only triggered for the 5 new items that have been added?

那就是:

ref.limitToLast(5)...

但这要求您知道自上次收听以来已将多少项添加到列表中.

But this requires that you know how many items were added to the list, since you last listened.

更常见的解决方案是跟踪您已经看到的最后一个项目,然后使用 startAt() 从您上次看到的位置开始触发事件:

The more usual solution is to keep track of the last item you've already seen and then use startAt() to start firing events from where you last were:

ref.orderByKey().startAt("-Ksakjhds32139")...

然后您将保留您在共享首选项中看到的最后一个键.

You'd then keep the last key you've seen in shared preferences.

同样,您可以通过以下方式保留上次可见活动的时间:

Similarly you can keep the last time the activity was visible with:

long lastActive = new Date().getTime();

然后向每个项目添加带有 Firebase.ServerValue.TIMESTAMPtimestamp,然后:

Then add a timestamp with Firebase.ServerValue.TIMESTAMP to each item and then:

ref.orderByChild("timetstamp").startAt(lastActive+1)...

这篇关于为新数据添加了 Firebase onChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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