Firebase Android - how to find out if a node is synchronized

I am using Firebase for Android for the chat component of our application. I'm having trouble figuring out how to reliably implement status updates in every chat message. For example, showing "Sending .." when the chat is synchronized with the server and has feedback with success after synchronization.

I have an onChildAdded listener that sends messages to my adapter. However, this listener starts immediately when each node is added locally and I cannot check the status of each node

My current solution is to save a set of node keys and add keys whenever I click on Firebase. Then in the setValue callback, I remove the node key from the set. However, this is very unreliable, as the nodes can synchronize when the calling activity has been destroyed, etc.

I am wondering if there is an easier way to check if each node is synchronized with the server?

Thanks!

+5
source share
1 answer

From the Firebase documentation when writing data :

If you want to know when your data was made, you can add a completion listener. Both setValue () and updateChildren () accept an optional completion listener, which is called when the record was committed to the database.

With this handy code example:

ref.setValue("I'm writing data", new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { System.out.println("Data could not be saved. " + firebaseError.getMessage()); } else { System.out.println("Data saved successfully."); } } }); 
+6
source

Source: https://habr.com/ru/post/1232049/


All Articles