3

I use Firestore (from Firebase) and I have tried, without success, to disable the usage of the cache.

Here is my problem. If I disconnect my device from the internet and that I execute this ...

getFireStoreInstance()
    .collection(PATH)
    .document(myObject.id)
    .set(myObject)
    .addOnCompleteListener( ... )

or this ...

getFireStoreInstance()
    .collection(PATH)
    .batch()
    .update(...)
    .commit()

... nothing happens (which is good because I don't have any internet connection). However, as soon as I reconnect my device to the internet, the completionListener is called to notify me that the action has been completed.

What I would like is that, when my device does not have access to the internet, the action fails and that the completionListener is called with failure. Is that possible?

Of course, I have tried the following code (from Firestore doc) but it looks to have no effect for writing (it has effect for reading though).

val settings = FirebaseFirestoreSettings.Builder()
        .setPersistenceEnabled(false)
        .build()

FirebaseFirestore.getInstance().firestoreSettings = settings

Thank you in advance

1 Answer 1

1

Listeners don't ever fail due to network connectivity, and you can't change that behavior. They always silently retry. The whole point of having a resilient SDK like this is to prevent perceived disruption in your app when its network connection is flakey - and that happens a LOT with mobile apps.

If you require for something to fail due to lack of connectivity, make an HTTP (or callable) endpoint with Cloud Functions, and call that directly. The call will fail if the HTTP socket can't connect.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. So I'll handle that myself in the code
What about using a Transactions for this, it will fail if internet is gone and the completionListener is called with failure
@ExocetKid You could, but transactions are inefficient, and not always the best way to perform a write. It's up to you, but I wouldn't use a transaction unless I actually need an atomic write.
I'm leaning towards that if cache setting is off (set to persistenceEnabled: false), the pending writes feature should not be enabled, but throw. Example: An admin user removes a customer product: Cache enabled -> user sees change and thinks it's removed, Cache disabled -> user assumes write failed but takes place later. If I understand all this correctly. If cache is off, it should be off for read and write and throw, since admin user and customer will never look at the same data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.