Skip to content
Advertisement

How to manage Offline Update state of document in Firestore database withing time interval

I’ve created an application in which I’ve used a Firestore database. Now my tasks are this, User can take action of update document within time duration.
I mean if the user tried to update while online mode then there is no problem But if the user device have connection issue of poor network connection then this update action should be trigger within 20 seconds to save on the Firestore server. If a user can’t do within time duration then the last action should be rolled out I mean cache action should not be push after 20 seconds

For Offline mode restrictions, I’ve set this setPersistenceEnabled(false) But still document update are sending which there is an issue with a network connection.

In the Realtime Database, there is one way to find out device connectivity https://firebase.google.com/docs/database/android/offline-capabilities

Is there any way in Firestore?
I’ve tried to disable/enable Network mode but it will not help me to solve this issue
https://cloud.google.com/firestore/docs/manage-data/enable-offline#kotlin+ktxandroid_4

db.disableNetwork
db.enableNetwork

I need this functinality in Android

I’ve tried this code to clear my ongoing request

val hashMap = HashMap<String, Any>()
                        hashMap["status"] = "ACCEPTED"
                        hashMap["time"] = time
                        app().firestoreDB
                            .collection("doc")
                            .document("id")
                            .update(hashMap)
                            .addOnSuccessListener {
                                isStoredOnServer = true
                                // my action 
                            }

            object : CountDownTimer(20000, 1000) {
                    override fun onTick(millisUntilFinished: Long) {
                        if(isStoredOnServer){
                            this.cancel()
                        }
                    }
                    override fun onFinish() {
                        if(!isStoredOnServer) {
                            FirebaseFirestore.getInstance().clearPersistence()
                                .addOnFailureListener { e -> Log.d("Firestore", "Error persistence writing document $e") }
                                .addOnSuccessListener { Log.d("Could enable persistence:") }

                        }
                    }
                }.start()

Advertisement

Answer

But if the user device have connection issue of poor network connection then this update action should be trigger within 20 seconds to save on the Firestore server. If a user can’t do within time duration then the last action should be rolled out, I mean cache action should not be push after 20 seconds

IMHO, I cannot see any benefit of implementing this mechanism since Firestore has its own offline persistence mechanism. According to the official documentation:

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.

So while offline, you can continue using the app. Besides that, for Android and iOS, offline persistence is enabled by default. Using:

setPersistenceEnabled(false)

It means that set the PersistenceEnabled option to false, meaning that you disable the entire persistence feature.

In the Realtime Database there is one way to find out device connectivity

In the case of Cloud Firestore, the same rules apply.

Is there any way in Firestore?

Simply keep the offline persistence enabled by removing the above line of code. For more info, please also see my answer from the following post:

Firestore offline data: Merging writes, maximum time of offline persistence

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement