1

I'm using the following code to download a file from Firebase storage:

[Source: https://firebase.google.com/docs/storage/android/download-files]

islandRef = storageRef.child("images/island.jpg");
File localFile = File.createTempFile("images", "jpg");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
        // Local temp file has been created
    }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

However, I've noticed that Firebase keeps retrying when there is no Internet.

That's why I've also tried:

mStoragePath.setMaxDownloadRetryTimeMillis(60000);
mStoragePath.setMaxOperationRetryTimeMillis(60000);
mStoragePath.setMaxUploadRetryTimeMillis(60000);

But this is just setting the maximum timeout value. Is it possible to fail the operation in a single try.

Thanks.

KENdi
  • 7,576
  • 2
  • 16
  • 31
DR93
  • 463
  • 6
  • 21
  • Why to you want it to fail after first try? Probably a solution might be to just check if you have network connection and react to it. – Simon B. Sep 11 '17 at 13:44

1 Answers1

1

This is working as intended: the failure listener executes when the task has failed. The fact that there currently is no connection is no reason for (immediate) failure, since it might just be an network glitch or even (less likely) a brief interrupt in Google Cloud Storage.

If you want to explicitly, immediately fail when there is no network connection, you will have to detect that condition explicitly in your code. See the Android documentation on detecting network condition or this answer:

ConnectivityManager cm =
        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
                      activeNetwork.isConnectedOrConnecting();

But you should really consider if this behavior is most helpful to your users in the above conditions.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807