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

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

问题描述

如果我的firebase参考文件中存有50,000个项目的列表,并且自上次客户端联机并侦听以来已添加5个项目,则必须使用哪个回调,以便只触发对于已添加的5个新项目,我已经在客户端上启用了使用 Firebase.getDefaultConfig()。setPersistenceEnabled(true)的离线持久性。 ; 。我有一个监听器绑定到一个活动监听添加到引用的孩子。每次创建活动时,都会为参考中的所有数据调用 onChildAdded 。是否有可能使我的本地缓存和数据之间的diff调用onChildAdded 和 onChildRemoved Firebase服务器?或者,如果这是不可能的,那么从firebase最近更新之后,只触发 onChild * 回调?

解决方案


每次创建activity时都会为参考中的所有数据调用onChildAdded。是否有可能使onChildAdded和onChildRemoved被调用,只为我的本地缓存和Firebase服务器上的数据之间的差异?

不,这是不可能的。从事件类型文档


child_added会为每个现有的子元素触发一次,然后每当新的子元素添加到指定的路径时再触发一次。 $ b

现在回到最初的问题:


不得不使用这样的方式,只有在添加了5个新项目的情况下才会触发它。


那将是:

  ref.limitToLast(5)... 

但是这要求您知道自上次收听以来已添加了多少项目。

更常见的解决方案是保留跟踪你已经看到的最后一个项目,然后使用 startAt()开始从你最后一个地方开始的事件:

  ref.orderByKey()。startAt( -  Ks akjhds32139)... 

然后,您将保留您在共享偏好设置中看到的最后一个键。



同样,您可以保留最后一次看到的活动:

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

然后用<$ c添加一个 timestamp $ c $ Firebase.ServerValue.TIMESTAMP 给每个项目然后:
$ b $ pre $ ref.orderByChild( timestamp)。startAt(lastActive + 1)...


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?

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?

解决方案

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 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:

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

That would be:

ref.limitToLast(5)...

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

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();

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

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

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

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