【问题标题】:Firebase storage handling network interruptions when download in progressFirebase 存储在下载过程中处理网络中断
【发布时间】:2016-09-26 17:24:04
【问题描述】:

我正在尝试从 Firebase 存储中下载一些文件。当有稳定的互联网连接时,它工作得很好。但是,如果在下载内容中途丢失了互联网连接,它只会继续尝试下载内容。如何检测是否没有下载内容?

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

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

【问题讨论】:

    标签: android download firebase interruption firebase-storage


    【解决方案1】:

    上传、下载和其他操作旨在自动重试并保护您免受临时中断。 如果在该时间内无法传输数据包,则有一个可配置的超时将结束(失败)操作。 你可以这样设置:

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

    上传、下载等操作有不同的超时时间。

    通过上传,您可以尝试从上次中断的地方继续上传(如果您的源是文件),即使上传被中断。为此,您需要从上传任务中获取“上传会话 uri”。

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

    【讨论】:

    • 上传/下载默认超时时间为 10 分钟,其他操作(例如 getDownloadUrl)默认超时时间为 2 分钟
    • 对于 2017 年偶然发现此问题的任何人,以下内容对我有用:myRef.storage.maxDownloadRetryTime = 2
    • 在 addOnFailureListenr 中显示空 sessionUri。
    • 尝试从 onProgress 监听器@ajaydhadhal 获取“uploadSessionUri”
    • 我设置为 3 秒但操作永远不会失败
    猜你喜欢
    • 2016-10-14
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    相关资源
    最近更新 更多