adarsh
adarsh

Reputation: 1141

Right way to handle Firebase DatabaseReference updateChildren OnCompletionListener on Android

I am using Firebase database and want that offline persistence should also work. I have seen that when calling DatabaseReference.updateChildren in offline mode, value listener callbacks are triggered immediately, while the OnCompletionListener triggers only when the network is back. Is that the correct understanding? If so, what are clients expected to do in case of success or failure in OnCompletionListener.

Specifically -

  1. If the OnCompletionListener gets a failure, is the change rolled back locally and all value-listener-callbacks fired with the rolled back value?

  2. In what scenarios could the OnCompletionListener return failure? Is there a case which needs to be retried?

  3. What would be a typical usecase to listen to OnCompletionListener's success instead of callback in ValueChangeListener?

Upvotes: 0

Views: 439

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

Is that the correct understanding?

Yes, it is.

When there is a loss of network connectivity (there is no network connection on user device), neither onComplete() nor onFailure() are triggered. This behavior makes sense, since the task is only considered completed when the data has been committed (or rejected) on the Firebase server.

  1. There is no rolled back because when data is updated, it is first written to the local version of the database, obviously when the persistence is enabled using the following line of code:

    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    
  2. When Firebase servers reject the update due the insufficient permissions caused by Firebase Security rules.

  3. This is the typical usecase, to listen to OnCompletionListener's success.

Upvotes: 2

Related Questions