Firebase在Android中的适配器中添加监听器 [英] Firebase adding listeners in adapters in Android

查看:138
本文介绍了Firebase在Android中的适配器中添加监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firebase很新,我正在试着围绕它。
我有这个适配器每次创建时注册到 ValueEventListener 。如果我不分离它,当我旋转我的电话和适配器被破坏/重建在片段?听众加起来?或是firebase足够聪明,知道这个特定的侦听器已经存在?



PS :我试图取消注册这个监听器在 onPause 使用它的片段的方法,但firebase似乎是删除我的缓存,所以在片段旋转后,需要一段时间才能重新获取数据,这是以前没有发生过。

解决方案

好的问题。所以,有几点需要注意:


  1. 如果您附加了您的监听器这个地方 onResume ,它会重新初始化你的监听器。设置侦听器时,会触发该特定节点的所有事件。不过,我仍然会在 onPause onResume

    中注册和取消注册我的Firebase参考。 / li>
  2. 您可以拥有任意Firebase侦听器的多个实例


    Firebase是否足够聪明地知道这个特定的侦听器已经存在?

    Firebase 意识到侦听器已经存在,并且不会发送两次相同的事件。但是,在旋转时,您正在创建侦听器的新实例。 Firebase无法将其视为相同的实例化侦听程序。因此,您将再次收到所有数据。 Firebase缓存所有数据。

  3. 当一个片段被附加并且侦听器被设置时,firebase将会进行两个主要的调用 -

    $ ul
  4. 第一个 - 用来检索缓存的查询数据。第二个 - 查询远程数据。首先调用缓存是很好的,因为在网络缓慢到没有的情况下它仍然可以工作。 现在,在这里裸露出来...... 当Firebase从在线服务器接收到快照时,它将对远程对象和本地对象进行复杂的评估。 Firebase将尽其最大能力使用复杂的ID来合并对象,这些ID使用时间戳和黑魔法[需要的来源]。有了这个新的快照,如果需要,它会将其保存到服务器。然后,只有当Firebase不同于缓存版本时,Firebase才会为您提供日期,并且相对于提供上述数据的侦听器实例而言,发生更改。此缓存驱动结构甚至适用于您保存你的数据:




    • 首先保存到缓存。

    • 第二个触发器回调。第三次尝试保存到服务器。

















    • $ b $ h $如果您将侦听器附加到Firebase onPause / onResume / code>,您将再次收到所有数据。唯一不能再接收它的方法是维护同一个监听器实例。

      除了维护侦听器实例外,我还使用了另一个解决方案。在我看来,我不喜欢它。但仍然是我最经常使用的。我做的是

      $ ul

    • 我将保留一个 final List< String> ,名为 ignoredList 。这个列表将由字符串键构建,这将是您的适配器中已有的对象的关键字。然后在 onPause 中添加这个数据到我的 ignoredList / code>,并将 childEvent 侦听器清空。 onResume 回调后,我设置了一个新的 childEvent code> listener。

    • 在事件侦听器的 onAdded 上,我检查新添加的对象与我的列表。如果我有它,我会从列表中删除它,没有别的。基本上忽略它。如果对象不在我的 ignoredList 中,我会像正常一样处理它。如果我从 onAdded 之外的其中一个回叫接收到它(例如 onRemoved onChanged 或者 onMoved ),那么我将把这个事件改变成列表中的那个对象,并从 ignoredList


      现在,我承认这不是最好的解决方案。如果两个源正在修改相同的DataSnapshot,则您可能会看到不正确的数据。这将是一个小机会,但完全有可能。幸运的是,如果数据集不准确,则不会保存到Firebase。


      我总是积极地寻找更好的策略,如果我找到一个,我会分享。与此同时,这个解决方案已经完美地为我的应用程序工作。

      I'm pretty new at firebase and I'm trying to wrap my head around it. I have this adapter that is registering to a ValueEventListener every time it is created. If I don't detach it, will the listeners add up as I rotate my phone and the adapter gets destroyed/rebuilt in the fragment?. Or is firebase smart enough to know that this particular listener already exists?

      PS: I tried to unregister this listener in the onPause method of the fragment using it, but firebase seems to be removing my cache so after the fragment rotates it takes a while to fetch the data again, that didn't happen before.

      解决方案

      Good question. So, few things to note:

      1. Where are you attaching your listener? If you are attaching this anywhere but the onResume, it will re-initialize your listener. When setting a listener it fires all events for that particular node. However, I still do all my registering and unregistering to my Firebase reference in onPause, and onResume

      2. You can have multiple instances of any Firebase listener.

        Is firebase smart enough to know that this particular listener already exists?

        Firebase is aware that listener already exists and will not send the same event twice. However, when rotating you are creating a new instance of your listener. Firebase cannot see this as the same instanced listener. Therefore, you receive all data again.

      3. Firebase caches all of the data. When a fragment is attached and the listener is set, firebase will make two main calls -

        • First - A query to retrieve cached data.

        • Second - A query to the remote data.

        Calling cache first is nice because it still works in cases with slow to no network. Now, bare with me here... When Firebase receives that snapshot from the the online servers it will do a complex evaluation of the remote object and local object. And to the best of its ability, Firebase will merge the objects using a complex ID that utilizes timestamps and black magic [source needed]. With this new snapshot, if needed, it will save it to the servers. Then, **Firebase will provide the date to you only if it differs from the cached version and changes relative to the instance of the listener that provided said data. This cache-driven structure even applies to when you save your data:

        • First- save to cache.

        • Second- trigger callback.

        • Third- attempt to save to server.


      To answer the question

      If you are attaching your listener to Firebase onPause/onResume, you will receive all data again. The only way to not receive it again is to maintain the same instance of that listener.

      In addition to maintaining my listener instance, I also have used another solution. In my opinion, I am not fond of it. But still is what I use most often. What I do, is

      • I will keep a final List<String>, called ignoredList. This list would be built of a String key, which would be the key for the object you already have in your adapter.

      • Then, in onPause I will add this data to my ignoredList and null out the childEvent listener.

      • After the onResume callback I set a new instance of childEvent listener.

      • On the onAdded of the event listener I check the newly added object against my list. If I have it, I will remove it from the list and nothing else. Essentially ignoring it. If the object is not in my ignoredList I will handle it like normal. If I receive it from one of the call backs other than onAdded, (i.e. onRemoved onChanged or onMoved) then I will make that event change to that object in the list and remove from ignoredList.

      Now, I admit this is not really the prettiest solution. You could see incorrect data if two sources were modifying the same DataSnapshot. It would be a small chance, but entirely possible. Luckily if the data sets were to fall inaccurate, it will not save to Firebase.

      I am always actively looking for better strategies for this, and if I find one I will share. In the meantime, this solution has been working perfectly for my apps.

      这篇关于Firebase在Android中的适配器中添加监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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