Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Firestore - get synchronization status on Android

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.

like image 234
jBegera Avatar asked Nov 15 '25 11:11

jBegera


1 Answers

Response from Firebase Support

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...
            }
        }

    });
like image 136
jBegera Avatar answered Nov 18 '25 02:11

jBegera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!