Firebase storage handling network interruptions wh

2020-02-06 05:03发布

问题:

I am trying to download some files from the firebase storage. It works well when there is stable internet connection. But if the internet connection is lost while downloading the content halfway, it just keeps trying to download the content. How to detect if there are no content being downloaded?

I have implemented the onProgessListener of StorageReference. However, I am not sure how to make use of it to detect if there are no progress in the download.

new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
        //What to do with the taskSnapshot to detect if there are no progress in the download?
    }
};

回答1:

Uploads, Downloads and other operations are intended to automatically retry and shield you from temporary interruptions. There is a configurable timeout that will end (fail) the operation if it cannot transfer a packet in that time. You can set this with:

//if we get interrupted for more than 2 seconds, fail the operation.
FirebaseStorage.getInstance().setMaxUploadRetryTimeMillis(2000);

There are different timeouts for uploads, downloads and other operations.

With uploads, you can attempt to resume the upload from where you left off (if your source was a file) even if it was interrupted. To do this you need to obtain the "upload session uri" from the upload task.

task.addOnFailureListener(new OnFailureListener {
  @Override
  public void OnFailure(Exception error) {
     Uri uploadsession = task.getSnapshot().getUploadSessionUri();
     //if you want to retry later, feed this uri as the last parameter
     // to putFile(uri, metadata, uri)
  }
}