Reputation: 1384
1 Suppose that i am doing something like following
FirestoreDatabase.getInstance()
.collection("users")
.add(userData); // where userData is reference to UserData object which contains many fileds
Now if i am showing that data in recyclerview , the client library will show that immediately in recyclerview even before it is saved in firestore database to keep my app more responsive.
So when the data shows up in the recyclerview the user thinks he successfully added the data BUT what happens if the write request fails when the data reach the firestore , will the onFailure
listner trigger and as the data already in the UI before actual write wil the data disappear from the UI ?
2 When the user is offline he can still use the app and add the data and shows up in UI for user it means he successfully added the data then whey onSuccessListener
gets triggered only when the user is online.
ex supposein successlistener in am displaying a toast saying data added but user only sees it when he is online , when he is offline he can see the data in UI but not toast and thinks why toast didn't show up this time.
Upvotes: 1
Views: 753
Reputation: 1001
This is because Firestore data is locally persisted by default. Therefore if you save data in Firestore it saves it in the local database. And later on when the internet will be available it'll be synced with the server.
If you want to get rid of that use this line before any operation takes place in application. More suitable place is Application class.
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
.setPersistenceEnabled(true)
.build();
db.setFirestoreSettings(settings);
After adding these lines the data fail to save in Firestore if an internet connection is not available.
Upvotes: 0
Reputation: 138824
BUT what happens if the write request fails
It means that don't have access to write that data. onFailure()
is only executed when there was a problem with writing the document, meaning the write operation is rejected.
When the user is offline he can still use the app and add the data and shows up in UI
This is happening because, for Android and iOS, offline persistence is enabled by default.
onSuccessListener gets triggered only when the user is online.
Yes, onSuccessListener()
gets triggered only when the write operation is successful, meaning the operation is written on Firebase servers.
upposein successlistener in am displaying a toast saying data added but user only sees it when he is online , when he is offline he can see the data in UI but not toast and thinks why toast didn't show up this time.
The user won't be notified with that toast, because that data is not yet added on the server.
Upvotes: 2
Reputation: 1169
The onSuccessListener
will trigger even if the user is offline because by default, the change is made locally. If it can't be persisted online immediately, the app will try again when a connection is available. This is explained here: Access Data Offline
Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data. When the device comes back online, Cloud Firestore synchronizes any local changes made by your app to the Cloud Firestore backend.
I don't know what situation you mean other than a network connection being unavailable for the data failing to be saved to the database, but the database will be cached locally so any conflicts should occur locally too.
EDIT: Fundamentally, if the data would fail to go into the database for any reason other than waiting for an internet connection, it won't appear in the UI which is being populated from the database, so you don't need to worry about things disappearing later.
Upvotes: 2