0

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()
Bhavin Chauhan
  • 1,950
  • 1
  • 26
  • 47

1 Answers1

1

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

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • thanks for your suggestions but I need this action in particular time. that's why I restrict PersistenceEnabled. Still in poor connection it save after few second – Bhavin Chauhan Aug 31 '20 at 07:57
  • my task is that I need to do this action withing time if time gone then don't need to send this on firebase server – Bhavin Chauhan Aug 31 '20 at 07:58
  • I've que list users so if there is any way to check online mode like real time database then suggest me – Bhavin Chauhan Aug 31 '20 at 07:59
  • *Still in poor connection it save after few second* yes, if you have a poor connection, the data is saved in the cache and once you regain connection, everything will be saved in the cloud. Even if you need some time or no time, the cache should be enabled and choose what to store or not directly on the client. I think **[this](https://stackoverflow.com/questions/49068084/about-firestore-is-there-some-flag-i-can-check-if-the-data-is-on-off-line-data)** might also help. – Alex Mamo Aug 31 '20 at 08:15
  • PersistenceEnabled is really good for offline saving data. But I my case it send one request to many user one by one if user not claim that action with time then we need to ignore that action – Bhavin Chauhan Aug 31 '20 at 09:07
  • That's a straightforward mechanism. So, you can send as many requests as you need and cancel them when the time is up. This can be simply achieved with the use of Cloud Functions, right? – Alex Mamo Aug 31 '20 at 09:11
  • Yes You are right I can do it with this but issue is that My Cloud Functions have to notify once action was accepted but there was delay to store. Is there any other way during save request. like timeout or remove offline mode or something else? – Bhavin Chauhan Aug 31 '20 at 10:36
  • No, it, if the request is accepted, then do some logic, otherwise, delete the requests after a specific amount of time. You can use [Cloud Scheduler](https://cloud.google.com/scheduler/) as explained in my answer from this **[post](https://stackoverflow.com/questions/54767485/is-it-not-possible-to-have-a-code-in-the-background-who-will-be-called-every-24h/54767571#54767571)**. – Alex Mamo Aug 31 '20 at 10:40
  • Alex Mamo thanks for reply. Is there any way to cancel request? like this onDisconnectRef.cancel() https://firebase.google.com/docs/database/android/offline-capabilities – Bhavin Chauhan Sep 01 '20 at 04:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220769/discussion-between-bhavin-chauhan-and-alex-mamo). – Bhavin Chauhan Sep 01 '20 at 05:14
  • I think you can use a [CancellableTask](https://firebase.google.com/docs/reference/android/com/google/firebase/storage/CancellableTask), where you can find a [cancel()](https://firebase.google.com/docs/reference/android/com/google/firebase/storage/CancellableTask#cancel()) method. – Alex Mamo Sep 01 '20 at 06:45
  • Alex thanks for reply Can you please guide me how I use CancellableTask with my firestore update operation with particular time? – Bhavin Chauhan Sep 02 '20 at 05:59
  • I've tried to cancel last offline data after complete that time using FirebaseFirestore.getInstance().clearPersistence(). But it rerun exception can't cancel on going data – Bhavin Chauhan Sep 02 '20 at 06:00
  • @BhavinChauhan Without seeing what you have tried in code, I cannot be much of a help and I recommend you post a new question using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Sep 02 '20 at 06:18
  • added code can you please help me how to cancel last request? I am not sure this is good pattern but I need this action within time if user can't done then need to cancel it in way without post on server – Bhavin Chauhan Sep 02 '20 at 06:51
  • @BhavinChauhan I still think that you should post a new question for this issue and explain in detail what exactly doesn't work the way you expect. – Alex Mamo Sep 02 '20 at 06:54
  • Okay post new question thanks https://stackoverflow.com/questions/63700993/how-to-restict-timely-manner-update-document-in-firestore-database-with-poor-net – Bhavin Chauhan Sep 02 '20 at 11:43
  • @BhavinChauhan I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo Sep 02 '20 at 12:12