如何在出现网络错误时缩短Firebase存储下载重试时间 [英] How to shorten Firebase storage download retrying period on Network Error

查看:61
本文介绍了如何在出现网络错误时缩短Firebase存储下载重试时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些基本的工作代码,可用于从Firebase存储中下载文件.

I have basic working code for downloading file from Firebase storage.

String key = "gs://.../test.jpg";
File file = new File(getCacheDir() + File.separator + "test.jpg");

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference gsRef = storage.getReferenceFromUrl(key);

gsRef.getFile(file)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>()
{
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot)
    {
        Log.d("APP", "onSuccess");
    }
}).addOnFailureListener(new OnFailureListener()
{
    @Override
    public void onFailure(@NonNull Exception exception)
    {
        Log.d("APP", "onFailure: ", exception);
    }
});

但是,如果在设备未连接到Internet的情况下执行了以上代码,则最终触发onFailure事件需要大约10分钟的时间.同时,日志中充满了重复的重试:

However, if above code is executed while device is not connected to Internet it takes almost 10 minutes before onFailure event is finally triggered. In the meantime log is filling up with repeated retries:

02-27 21:41:07.203 12954-13288/com.example.test E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.FirebaseException: An internal error has occurred. [ <<Network Error>> ]
02-27 21:41:08.244 12954-13288/com.example.test W/ExponenentialBackoff: network unavailable, sleeping.
02-27 21:41:08.294 12954-13288/com.example.test E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.FirebaseException: An internal error has occurred. [ <<Network Error>> ]
02-27 21:41:09.405 12954-13288/com.example.test W/ExponenentialBackoff: network unavailable, sleeping.
02-27 21:41:09.485 12954-13288/com.example.test E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.FirebaseException: An internal error has occurred. [ <<Network Error>> ]

有没有一种方法可以缩短(自定义)重试时间并更快地触发onFailure事件?

Is there a way I can shorten (customize) retry period and trigger onFailure event sooner?

推荐答案

是的,您可以使用setMaximum{OPERATION}RetryTimeMillis()方法配置上传,下载和其他操作的超时时间:

Yes, you can configure the timeout for uploads, downloads, and other operations using the setMaximum{OPERATION}RetryTimeMillis() methods:

FirebaseStorage storage = FirebaseStorage.getInstance();
storage.setMaxDownloadRetryTimeMillis(60000);  // wait 1 min for downloads
storage.setMaxOperationRetryTimeMillis(10000);  // wait 10s for normal ops
storage.setMaxUploadRetryTimeMillis(120000);  // wait 2 mins for uploads

请参见参考更多文档

这篇关于如何在出现网络错误时缩短Firebase存储下载重试时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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