Firebase Storage: cancel a task / download
casaucao opened this issue · 2 comments
casaucao commented
As you probably know, method getFile()
in Firebase Storage returns StorageTask<FileDownloadTask.TaskSnapshot>
which extends from CancellableTask
. Both classes are cancellable, which means we could cancel the current download.
Sample code:
StorageTask fileDownloadTask = someFileStorageRef.getFile(someFile).addOnSuccessListener(taskSnapshot -> {
doStuff();
});
// Somewhere in your project a user wants to cancel this download.
fileDownloadTask.cancel();
This feature would be quite interesting in order to let a user cancel the download.
FrangSierra commented
You are totally right, I'm gonna change the implementation for all the storage calls which can be canceled to something like :
@NonNull
public static Maybe<FileDownloadTask.TaskSnapshot> getFile(@NonNull final StorageReference storageRef,
@NonNull final Uri destinationUri) {
return Maybe.create(new MaybeOnSubscribe<FileDownloadTask.TaskSnapshot>() {
public void subscribe(final MaybeEmitter<FileDownloadTask.TaskSnapshot> emitter) throws Exception {
final StorageTask<FileDownloadTask.TaskSnapshot> taskSnapshotStorageTask =
storageRef.getFile(destinationUri).addOnCompleteListener(new OnCompleteListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<FileDownloadTask.TaskSnapshot> task) {
if (task.isComplete()) {
emitter.onSuccess(task.getResult());
} else {
emitter.onError(task.getException());
}
emitter.onComplete();
}
});
emitter.setCancellable(new Cancellable() {
@Override public void cancel() throws Exception {
taskSnapshotStorageTask.cancel();
}
});
}
});
}
I will fix it this weekend and upload a new version.Thanks for your feedback!
FrangSierra commented
This change is already on production with the last release(1.4.0) Enjoy it!