Reputation: 1141
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 -
If the OnCompletionListener gets a failure, is the change rolled back locally and all value-listener-callbacks fired with the rolled back value?
In what scenarios could the OnCompletionListener return failure? Is there a case which needs to be retried?
What would be a typical usecase to listen to OnCompletionListener's success instead of callback in ValueChangeListener?
Upvotes: 0
Views: 439
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.
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);
When Firebase servers reject the update due the insufficient permissions
caused by Firebase Security rules.
This is the typical usecase, to listen to OnCompletionListener's success.
Upvotes: 2