Firebase storage handling network interruptions when download in progress

拥有回忆 提交于 2019-12-17 19:57:48

问题


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)
  }
}


来源:https://stackoverflow.com/questions/37499262/firebase-storage-handling-network-interruptions-when-download-in-progress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!