Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Android: File upload cancellation

Does uploadTask.cancel() work only with large files? I am trying to upload a file of which it works but also the file upload should be cancellable of which in my case it only works with large files small files upload even though I have cancelled the file upload.

like image 886
Gov-G Avatar asked Jan 16 '18 11:01

Gov-G


1 Answers

What is probably happening is that the small files are uploaded very fast and by the time you call cancel(), they have already been uploaded. You didn't provide any code, but I assume you're executing uploadTask.cancel() in the click of a button. So I recommend that you check if the task is completed before cancelling it. And if it is, delete the small file. You can use this code for that:

    if (!uploadTask.isComplete()) {
        //Upload is not complete yet, let's cancel
        uploadTask.cancel();
    } else {
        //Upload is complete, but user wanted to cancel. Let's delete the file
        uploadTask.snapshot.ref.delete();
        // storageRef.delete(); // will delete all your files
    }
like image 169
Rosário Pereira Fernandes Avatar answered Sep 20 '22 14:09

Rosário Pereira Fernandes