如何缩短networking错误的Firebase存储下载重试时间

我有从Firebase存储下载文件的基本工作代码。

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

但是,如果上面的代码在设备未连接到互联网的情况下执行,则在onFailure事件最终触发之前需要将近10分钟。 与此同时,日志中充满了重复的重试:

 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事件?

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

 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 

有关更多信息 ,请参阅参考文档 ,