我有从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);
}
});
但是,如果在设备未连接到Internet时执行上述代码,则最终触发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
事件?
答案 0 :(得分:7)
是的,您可以使用setMaximum{OPERATION}RetryTimeMillis()
方法配置上传,下载和其他操作的超时时间:
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