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

huangapple go评论70阅读模式
英文:

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

问题

我已经在一个应用程序中创建了一个使用了 Firestore 数据库的应用。
现在我的任务是,用户可以在一定的时间段内执行更新文档的操作。

我的意思是,如果用户在在线模式下尝试更新,那么就没有问题,
但是如果用户设备遇到网络连接问题,更新操作应该在 20 秒内触发,以便保存到 Firestore 服务器上。
如果用户无法在规定的时间内完成操作,那么最后的操作将被取消,我的意思是缓存操作在 20 秒后不应被推送。

对于离线模式的限制,我已经设置了:
setPersistenceEnabled(false) 但是文档更新仍然在发送,存在网络连接问题。

在实时数据库中,有一种方法可以确定设备的连接状态
https://firebase.google.com/docs/database/android/offline-capabilities

Firestore 中是否有类似的方法?
我已经尝试过禁用/启用网络模式,但这对于解决这个问题没有帮助。

db.disableNetwork
db.enableNetwork

我需要在 Android 中实现这个功能。

我尝试过以下代码来取消我的进行中的请求:

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

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()
英文:

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.<br/>
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?<br/>
I've tried to disable/enable Network mode but it will not help me to solve this issue<br/>https://cloud.google.com/firestore/docs/manage-data/enable-offline#kotlin+ktxandroid_4

db.disableNetwork<br/>
db.enableNetwork<br/>

I need this functinality in Android <br/>

I've tried this code to clear my ongoing request

val hashMap = HashMap&lt;String, Any&gt;()
                        hashMap[&quot;status&quot;] = &quot;ACCEPTED&quot;
                        hashMap[&quot;time&quot;] = time
                        app().firestoreDB
                            .collection(&quot;doc&quot;)
                            .document(&quot;id&quot;)
                            .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 -&gt; Log.d(&quot;Firestore&quot;, &quot;Error persistence writing document $e&quot;) }
                                .addOnSuccessListener { Log.d(&quot;Could enable persistence:&quot;) }

                        }
                    }
                }.start()

答案1

得分: 1

> 但是如果用户设备遇到网络连接问题,网络连接质量差,那么这个更新操作应在20秒内被触发,以便保存在Firestore服务器上。如果用户不能在规定时间内完成,则应该取消最后的操作,我的意思是缓存操作在20秒后不应被推送。

依我之见,我无法看出实施这种机制有任何好处,因为Firestore有其自己的离线持久性机制。根据官方文档

> Cloud Firestore支持离线数据持久性。此功能会缓存您的应用程序正在积极使用的Cloud Firestore数据的副本,因此您的应用程序可以在设备离线时访问数据。

因此,在离线状态下,您可以继续使用该应用程序。此外,对于Android和iOS,默认情况下启用了离线持久性。使用:

setPersistenceEnabled(false)

这意味着将PersistenceEnabled选项设置为false,即禁用整个持久性功能。

> 在实时数据库中,有一种方法可以查明设备的连接情况。

在Cloud Firestore的情况下,适用相同的规则。

> 在Firestore中是否有任何方法?

只需通过删除上述代码行来保持启用离线持久性。有关更多信息,还请参阅我在以下帖子中的回答:

> https://stackoverflow.com/questions/47111181/firestore-offline-data-merging-writes-maximum-time-of-offline-persistence

英文:

> 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:

> https://stackoverflow.com/questions/47111181/firestore-offline-data-merging-writes-maximum-time-of-offline-persistence

huangapple
  • 本文由 发表于 2020年8月29日 21:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63647745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定