5

As stated in the title, I am using the Firebase Realtime Database, with persistence enabled.

As stated in the guide,

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache. Listener callbacks will continue to fire for local updates.

While this happens with the ValueEventListeners and every read listener, I can't find a way to make it work with write operations as well. This is a snippet from my database helper class:

    DatabaseReference userRef = root.child("users").child(user.getUid());
    userRef.setValue(user).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void result) {
            //handle success event
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            //handle failure event
        }
    });

Even with persistence enabled, that should make the database behave in a "reactive fashion", these two listeners will not fire until the database goes online again and finally pushes the updates to the server.

This isn't really what I was hoping for, since I expected the event to be triggered once the data was cached locally. I know I could simply ignore the events and pretend it succeeded, but I was wondering, am I missing something? Is there a way to trigger the events as soon as the data is cached?

aofdev
  • 1,772
  • 1
  • 15
  • 29
edornd
  • 441
  • 1
  • 4
  • 18

1 Answers1

2

The onSuccess listener of a write operation is triggered when the data is written to the database on the server.

There is no event that triggers when the data has been written to the local queue.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Alright, looks like I have to pretend that it succeeds then! Is there any workaround I can use, apart from ignoring the result? I tried checking the connection state before adding the listeners, but if I lose the connection after I attached them (and the connection listener takes a while to update the state) it will behave same as before, waiting until the connection comes back in... I can't find a way to remove those listeners as well. – edornd Sep 02 '17 at 14:43
  • I'm not sure [what you're trying to accomplish]((http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem), which is why I answered how the system behaves. – Frank van Puffelen Sep 03 '17 at 12:46
  • @FrankvanPuffelen I have same problem. I'm showing a dialog fragment which required a response from OnSuccess or OnFailure to get close. But as you said there no callback when we write offline then we have to close dialog without waiting for response and pretend that write was successful – Zaid Mirza Mar 29 '19 at 16:54
  • What do you want to happen if the user is offline? If you want them to be redirected, it helps to realize that when the `userRef.setValue(user)` call completes, the write has been processed by the local app. You don't need a success listener for that. – Frank van Puffelen Mar 29 '19 at 16:59