I had started using Firebase Firestore database in my Android app. The main usecase of Firestore implementation is to create a bunch of documents in offline mode and then upload them when the device gets online. The problem is that "device online time" is limited, thus I need to provide the synchronization status indicator to the user.
When you write to Firestore while using offline data, the listener will be triggered when the data is stored locally and then again when it is stored on the server as mentioned here. A probably solution could be to show the user the current status of the data or a percentage of the items that were pushed to the server.
It's not a straightforward solution, but it works.
firestore.collection(COLLECTION)
.whereEqualTo(KEY, VALUE) // select documents created by particular device
.addSnapshotListener((querySnapshot, e) -> {
if (e != null) {
// ERROR
return;
}
boolean fromCache = querySnapshot.getMetadata().isFromCache();
if (!fromCache) {
// SYNC DONE
} else {
int connectivityStatus = NetworkUtil.getConnectivityStatus(getApplicationContext());
if (connectivityStatus == NetworkUtil.TYPE_NOT_CONNECTED) {
// WAITING ON CONNECTION...
} else {
// SYNCING...
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With