4

I am developing an Android app using Cloud Firestore to store data. This is how I set data to the database:

private fun setData() {
    val task = UploadDataTask()
    task.setOnUploadFinishedListener(object: UploadDataTask.OnUploadFinishedListener{
        override fun uploadFinished() {
            // Do something
        }

        override fun uploadFailed() {
            // Do something
        }
    })
}

private class UploadDataTask: AsyncTask<Void, Void, Void>() {

    private var onUploadFinishedListener: OnUploadFinishedListener? = null
    fun setOnUploadFinishedListener(listener: OnUploadFinishedListener) {
        onUploadFinishedListener = listener
    }

    override fun doInBackground(vararg params: Void?): Void? {
        val map = hashMapOf(
            UID to firebaseUser.uid
        )
        firebaseFirestore.collection(USERS)
            .document(firebaseUser.uid)
            .set(map)
            .addOnSuccessListener {
                if(onUploadFinishedListener != null)
                    onUploadFinishedListener!!.uploadFinished()
            }
            .addOnFailureListener {
                if(onUploadFinishedListener != null)
                    onUploadFinishedListener!!.uploadFailed()
            }
        return null
    }

    interface OnUploadFinishedListener {
        fun uploadFinished()
        fun uploadFailed()
    }
}

This works great, but there is one exception. When I want to load data to the Firestore, but there is no connection to the internet, neither the onSuccessListener nor the onFailureListener gets called. I know that this is because they only get called when the data is written to the Firestore. But I don't know of any other way to check if there is a connection or not. For example, when I want to show a progress dialog until the data is successfully written to the Firestore, it would not dismiss if there was no connection. So how can I check that?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Apri
  • 1,241
  • 1
  • 8
  • 33

1 Answers1

2

First thing first. The Cloud Firestore client already runs all network operations in a background thread. This means that all operations take place without blocking the main thread. Putting it in an AsyncTask does not give any additional benefits.

For example, when I want to show a progress dialog until the data is successfully written to the Firestore, it would not dismiss if there was no connection.

Simply by displaying the ProgressDialog once you call setData() method and dismiss it in onSuccess(). Since this method is called only when the data is successfully written on Firebase servers, that's the right place to use it.

Furthermore, if you want to have the same behaviour when you read data then you should use isFromCache() method like explained in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Okay, but when I have no internet connection, the dialog would still not be dismissed because `onSuccess()` would not be called. – Apri Nov 12 '19 at 10:38
  • Yes, that's correct. The `ProgressDialog` will be dismissed once you regain internet connection, when `onSuccess()` is triggered. Isn't this what you want? – Alex Mamo Nov 12 '19 at 10:44
  • No, I want it to be dismissed, when I have internet connection and `onSuccess()` or `onFailure()` is called, BUT if there is no internet connection, I also want it to be dismissed and than show a message or something else to inform the user that uploading the data didn't work. – Apri Nov 12 '19 at 10:46
  • If you want to be dismissed when you have an internet connection, then you should call `dismiss()` in `onSuccess()` **and** in `onFailure()`. **But** if you dismiss the `ProgressDialog` when you have no internet connection, then you'll have nothing to dismiss when you regain connection, right? Because the `ProgressDialog` is already dismissed. If you want to know if a device is connected to the internet, please see [Detect whether there is an Internet on Android](https://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android). Is it ok now? – Alex Mamo Nov 12 '19 at 11:01
  • Yes, thank you. I just wanted to know how to detect if there is internet connection. – Apri Nov 12 '19 at 11:08
  • 4
    I have the same problem and this thread does not answer the question. – eslamb Apr 02 '20 at 23:10