Good question. So a few notes:
Where do you attach your listener? If you attach it anywhere but onResume , it will reinitialize your listener. When setting up the listener, it fires all events for this node. However, I still do all my registration and registration in my Firebase link in onPause and onResume
You can have multiple instances of any Firebase listener.
Is firebase smart enough to know that this particular listener already exists?
Firebase knows that a listener already exists and will not send the same event twice. However, when you turn, you create a new instance of your listener. Firebase cannot see this as the same excited listener. Therefore, you again get all the data.
Firebase caches all data . When the fragment is connected and the listener is installed, firebase will make two main calls -
The cache call is good at first because it still works in cases with a slow network. Now, naked with me here ... When Firebase receives this snapshot from online servers, it will perform a complex assessment of the remote site and the local site. And as far as possible, Firebase will combine objects using a complex identifier that uses timestamps and black magic [source needed]. With this new snapshot, if necessary, it will save it to the servers. Then ** Firebase will only give you a date if it is different from the cached version and changes relative to the listener instance that provided the data. This cache-driven structure is even applicable when you save your data:
To answer the question
If you attach your listener to Firebase onPause / onResume , you will get all the data again. The only way not to get it again is to maintain the same instance of this listener.
Besides saving my listener instance, I also used a different solution. In my opinion, I do not like this. But still this is what I use most often. What am I doing,
I will save a final List<String> called ignoredList . This list will be built from the String key, which will be the key for the object that you already have in your adapter.
Then, in onPause , I will add this data to my ignoredList and nullify your childEvent .
After the onResume I set up a new instance of the childEvent listener.
In the onAdded event listener, I check the newly added object on my list. If I have, I will remove it from the list and nothing more. Essentially ignoring this. If the object is not in my ignoredList , I will process it as usual. If I get it from one of the callbacks, except onAdded (i.e. onRemoved onChanged or onMoved ), then I will make this event changed for this object in the list and remove it from ignoredList .
Now I admit that this is not the most beautiful solution. You could see incorrect data if two sources changed the same DataSnapshot. This would be a small chance, but quite possible. Fortunately, if the datasets were inaccurate, this will not save Firebase.
I am always actively looking for the best strategies for this, and if I find it, I will share it. In the meantime, this solution works great for my applications.
Chad bingham
source share