Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Android - how to tell if node has been synced

I'm using Firebase for Android for the chat component of our app. I'm having trouble figuring out how to reliably implement status updates on each chat message. For example, showing "Sending.." when the chat is being synced with the server, and having a success feedback after sync.

I have a onChildAdded listener that supplies the messages to my adapter. However, this listener is fired immediately when each node is added locally, and I can't check the status of each node

My Current solution is to keep a set of node keys, and add keys whenever I push something to Firebase. Then on the setValue callback, I remove the node key from the set. However, this is very unreliable since the nodes can be synced when the calling activity has been destroyed, etc.

I am wondering if there is a simpler way to check if each node has been synced to the server?

Thanks!

like image 261
recipherus Avatar asked Sep 22 '15 19:09

recipherus


People also ask

How do I know if my app is connected to Firebase?

If you want to know if the client is connected to the server before calling setValue() , you can attach a listener to . info/connected .

How do I know if Firebase is working?

However you can check if Firebase itself is working fine by checking Firebase Status Dashboard. In this site you can also find in what date Firebase service was not available or unstable in the past.

How can I check if a value exists already in a Firebase data class Android?

child(busNum). exists() tests for the existence of a value at location BusNumber/<busNum> . It will not be true unless busNum is one of the keys created by push() .

How do I get particular data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.


1 Answers

From the Firebase documentation on writing data:

If you'd like to know when your data has been committed, you can add a completion listener. Both setValue() and updateChildren() take an optional completion listener that is called when the write has been committed to the database.

With this handy code sample:

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.");
        }
    }
});
like image 142
Frank van Puffelen Avatar answered Sep 28 '22 01:09

Frank van Puffelen