I am using firebase to develop a small app, I read that the Persistence(true) will queue the requests and execute them when the users have a connection. I put some codes in OnCompleteListener but they will not be executed when the users are offline and the data will become wrong when the users go online (after restarting the app ). so, should I move the codes out of OnCompleteListener() ? When should I use OnCompleteListener ?

And when I open the app again . the requests execute but the OnCompleteListener does not fire. anyone, please?

1 Answers

1
Frank van Puffelen On Best Solutions

As you've found Firebase Realtime Database's OnCompleteListeners fire when the data has been written to the database on the server, and they are not persisted across app restarts.

I typically use OnCompleteListener to detect problems in writing, such as security rules rejecting the write operation.

You can also use OnCompleteListener to for example mark a messages as "delivered" to the database, but that requires some more work on your side to also handle the app restart scenario.

For example: writes of each client are executed in the order in which you call the API. So if you perform a token write when the app starts, and wait for the OnCompleteListener's success of that one, you can be certain that writes that were submitted before the app restart have completed.