6

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!

recipherus
  • 359
  • 1
  • 6
  • 12
  • 1
    I answered below, but next time please add the minimal code to reproduce the problem to your question. Code is a lot easier to parse than descriptions of code. See http://stackoverflow.com/help/mcve – Frank van Puffelen Sep 22 '15 at 19:33

1 Answers1

6

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.");
        }
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    Yes, this is currently the method I use. But what about in the instance where I have no network, then this listener is not called at all, since the data is queued to be written when network is turned on. In the meantime, the activity containing this callback is destroyed. Then, I turn on network, so the writes are finished. Then I go back to the activity. At this point the data is synced, but the callback is never fired. – recipherus Sep 22 '15 at 20:43
  • To clarify, the data is synced in the background while the activity has been closed by the user. Therefore, this callback is never received – recipherus Sep 22 '15 at 20:52
  • 2
    In the same session of the app, the listeners will usually still be around and be invoked. If you use disk persistence and have an app restarts, the listeners indeed won't survive the restart. – Frank van Puffelen Sep 22 '15 at 22:34
  • If there are some update on the same path in the server (update by other client i.e. Web app). Will this onComplete listener fire after data download and upload? – Shripad Bhat Oct 19 '17 at 23:09
  • since the code is deprecated, should replace with `new DatabaseReference.CompletionListener()` instead of `new Firebase.CompletionListener()` – August Nov 17 '17 at 03:10