简体   繁体   English

Firestore如何禁用与云数据库的自动同步?

[英]Firestore how to disable automatic sync with cloud database?

I noticed this behavior: 我注意到这种行为:

  1. Disconnect a device from the internet 断开设备与互联网的连接
  2. Create a few new documents while still offline 离线时创建一些新文档
  3. Close the app Then 关闭应用程序,然后
  4. Go online and reopen App 上网并重新打开App
  5. Documents are automatically synced with firestore (I lose control over completeListener) 文件会自动与Firestore同步(我无法控制completeListener)

Problem is that actions I had in onCompleteListener are not run. 问题是我在onCompleteListener中执行的操作未运行。 Like in this snippet (do some super important stuff) is not run in the described scenario, also OnFailureListener is not called when a user is offline, so I cannot tell if it went ok or not. 就像在描述的场景中未运行此代码段(做一些非常重要的事情)一样 ,当用户脱机时也不会调用OnFailureListener,因此我无法确定它是否正常运行。

FirebaseFirestore.getInstance().document(...).collection(...)
                    .set(message).addOnCompleteListener {
                     //do some super important stuff
                   }

I would rather do sync in this case on my own so I want it to fail and not repeat again. 在这种情况下 ,我宁愿自己进行同步所以我希望它失败并且不再重复。

How can I disable automatic sync of firestore?, for this one case only 仅在这种情况下,如何才能禁用Firestore的自动同步?

So I wanted to ask this question and somehow Firestore transactions got to me. 因此,我想问这个问题,然后以某种方式将Firestore交易转交给我。 So to fix this problem I used transactions ( firestore / realtime dbs ) 因此,为了解决此问题,我使用了事务( firestore / 实时数据库

Transactions will fail when the client is offline. 客户端脱机时事务将失败。

So how it works now. 所以现在如何运作。

I try to run the transaction. 我尝试运行事务。 A If it fails I store it into a database B If it succeeds I try to remove it from the database And on every app start, I run sync service (check unsynced dbs and insert missing) A如果失败,则将其存储到数据库中B如果成功,则尝试从数据库中删除它,并在每次启动应用程序时运行同步服务(检查未同步的dbs并插入丢失的内容)

val OBJECT = ...
val ref = FirebaseFirestore.getInstance().document(...).collection(...)

FirebaseFirestore.getInstance().runTransaction(
        object : Transaction.Function<Boolean> {

            override fun apply(transaction: Transaction): Boolean? {
                transaction.set(ref, OBJECT)
                transaction.update(ref, PROPERTY, VALUE)
                return true
            }
        })
          .addOnSuccessListener {
               println("Inserting $OBJECT ended with success==$it")
               //todo remove from dbs (unsynced messages)

          }.addOnFailureListener {
               it.printStackTrace()
               //todo save to dbs (unsynced messages)
                }

//They work similarly in realtime database //它们在实时数据库中的工作方式相似

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM