0

When updating or deleting documents or users in Cloud Firestore, it seems that Firebase caches these requests even in offline mode. How do I cancel these requests if there is a connection loss? I would rather not cache these updates as it would create a lot of problems in my app.

Below, it seems that no exceptions are caught when trying to execute this function in offline mode.

    fun deleteUser(){
            try {
                val user = Firebase.auth.currentUser!!
                val db = Firebase.firestore
                db.collection("users").document(user.uid)
                    .delete().addOnCompleteListener() {
                        if (it.isSuccessful) {
                            user.delete()
                                .addOnCompleteListener { task ->
                                    if (task.isSuccessful) {
                                        Log.d(TAG, "User account deleted.")
                                    }
                                }
                        }
                    }
            } catch (e : Exception){
                Log.d(TAG,e.toString())
            
                e.printStackTrace()
            }
        }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Are you entirely sure that "no exceptions are caught"? Your current code is ignoring them in each `OnCompleteListener` where you only handle `it.isSuccessful == true` rather than also handling it when it's false. – samthecodingman Jan 17 '22 at 12:01
  • Even so, simply being offline is not an error condition as it's designed to make your app usable on spotty connections like out in rural areas. What you should do is attempt to detect the time your app goes offline and then after about a minute, put your app into a proper offline mode where you no longer make changes to your database. The way you handle this is entirely dependent on what your app does. – samthecodingman Jan 17 '22 at 12:01

1 Answers1

0

I would rather not cache these updates as it would create a lot of problems in my app.

If don't want to cache the data, then simply disable this feature. According to the official documentation, in Kotlin it would be as simple as:

val settings = firestoreSettings {
    isPersistenceEnabled = false 
}
db.firestoreSettings = settings

it seems that no exceptions are caught when trying to execute this function in offline mode.

Firestore SDK doesn't throw an error when there is no internet connection, and it makes sense since Firestore is designed to work offline. Behind the scenes, Firestore SDK tries to reconnect until the devices regain connectivity. However, it will indeed be triggered, when Firestore servers reject the request due to a security rule issue.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193