Firebase 存储在下载过程中处理网络中断 [英] Firebase storage handling network interruptions when download in progress

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

问题描述

我正在尝试从 firebase 存储下载一些文件.当互联网连接稳定时,它运行良好.但是,如果在下载内容中途断开 Internet 连接,它只会继续尝试下载内容.如何检测是否没有内容被下载?

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?

我已经实现了 StorageReferenceonProgessListener.但是,我不确定如何利用它来检测下载是否没有进展.

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天全站免登陆