Cancelling an UploadTask to firebase storage

Viewed 1394

I would like to understand better the behavior of firebase.storage.StorageTask.cancel(), as documented here.

I'm creating an UploadTask with the intent to upload an image to Firebase Storage.

// Create an UploadTask
final UploadTask mUploadTask = storageRef.putFile(_photoUri);
    // Add Listeners to mUploadTask.
    mUploadTask_ChangeProfilePhoto
                    .addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
                            Log.d(TAG, "mUploadTask: onPaused: ");
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            Log.d(TAG, "mUploadTask: : onProgress: ");
                        }
                    })
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            Log.d(TAG, "mUploadTask: onSuccess");
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e(TAG, "mUploadTask: On Failure", e);
                        }
                    });

A random amount of milliseconds after UploadTask begins its job, I try to cancel the UploadTask:

// Attempt to cancel the UploadTask some seconds after it's started.
boolean canBeCancelled = mUploadTask.cancel();
Log.d(TAG, "mUploadTask CAN BE CANCELLED: " + canBeCancelled);

As mentioned here, canBeCancelled should be:

true if this task was successfully canceled or is in the process of being canceled. Returns false if the task is already completed or in a state that cannot be canceled.

What I expected when I tried to cancel mUploadTask:

If canBeCancelled is true, the UploadTask to get cancelled immediately or at least to get cancelled eventually.

What I got when I tried to cancel mUploadTask:

  • Case 1: If canBeCancelled is true, the UploadTask gets cancelled after 1-3 seconds, not immediately. Here is the timing as presented in the logcat.

Logcat when mUploadTask gets cancelled

  • Case 2: Although canBeCancelled is true, the UploadTask completes successfully after 1-3 seconds and it never gets cancelled.

Logcat when UploadTask completes successfully

The above situation gives rise to three questions:

  1. When mUploadTask.cancel() returns true and the UploadTask gets eventually cancelled (as in Case 1), is it normal to have a delay 1-3 seconds between issuing the cancel command and the cancelling of the UploadTask?
  2. When mUploadTask.cancel() returns true, does this give any indication that the UploadTask will get eventually cancelled or is it still possible for the UploadTask to complete or fail (as in Case 2)?
  3. Is there a better approach cancelling an UploadTask?

Thanx in advance for any input!

0 Answers
Related