Firebase存储处理网络中断时正在下载进行中 [英] Firebase storage handling network interruptions when download in progress

查看:181
本文介绍了Firebase存储处理网络中断时正在下载进行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从firebase存储中下载一些文件。当互联网连接稳定时,效果很好。但如果互联网连接丢失了一半下载内容,它只是不断尝试下载的内容。如何检测是否没有下载内容?

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?

我已经实现了 onProgessListener StorageReference 。但是,我不知道如何使用它来检测下载中是否没有进度。

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?
    }
};


推荐答案

上传,下载和其他操作旨在自动重试并防止您暂时中断。
如果在该时间内无法传输数据包,则会有一个可配置的超时结束(失败)操作。
您可以使用以下设置:

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.

通过上传,您可以尝试从停止的位置恢复上传(如果您的来源是文件),即使它已被中断。为此,您需要从上传任务中获取上传会话uri。

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

这篇关于Firebase存储处理网络中断时正在下载进行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆