简体   繁体   中英

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
  5. Documents are automatically synced with firestore (I lose control over completeListener)

Problem is that actions I had in onCompleteListener are not run. 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.

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

So I wanted to ask this question and somehow Firestore transactions got to me. So to fix this problem I used transactions ( firestore / realtime dbs )

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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