【发布时间】:2020-12-18 05:05:07
【问题描述】:
我创建了一个使用 Firestore 数据库的应用程序。
现在我的任务是这样,用户可以在一段时间内采取更新文档的操作。
我的意思是如果用户在在线模式下尝试更新,那么没有问题
但如果用户设备存在网络连接不良的连接问题,则应在 20 秒内触发此更新操作以保存在 Firestore 服务器上。
如果用户不能在持续时间内执行,则应推出最后一个操作,我的意思是缓存操作不应在 20 秒后推送
对于离线模式限制,我设置了这个 setPersistenceEnabled(false) 但仍在发送文档更新,网络连接存在问题。
在实时数据库中,有一种方法可以找出设备连接性 https://firebase.google.com/docs/database/android/offline-capabilities
Firestore 有什么办法吗?
我已尝试禁用/启用网络模式,但它无助于解决此问题
https://cloud.google.com/firestore/docs/manage-data/enable-offline#kotlin+ktxandroid_4
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
// 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()
【问题讨论】:
标签: java android firebase google-cloud-firestore