0

I have this method to download something from firebase storage, the thing is that the files are downloaded but fSucess is always false. i was trying moving logs to get where it puts false, but i dont know why the method always returns false but it does what is inside. Any idea? i want to return true when the two files were downloaded

public boolean DescargarArchivosPais(String locale){

        File rootPath = new File(context.getCacheDir(),"MY_FILES");
        if(!rootPath.exists()) {
            rootPath.mkdirs();//si no existe el directorio lo creamos
        }

        StorageReference mStorageRef2 = FirebaseStorage.getInstance().getReference().child("Files/y/" + "y_" + locale + "." + "txt");
        StorageReference mStorageRef1 = FirebaseStorage.getInstance().getReference().child("Files/x/" + "x_" + locale + "." + "txt");
        Log.e("REFERENCIAx",""+ mStorageRef1);
        Log.e("REFERENCIAy",""+ mStorageRef2);

        final File xFile = new File(rootPath, "x.txt");
        final File yFile = new File(rootPath, "y.txt");

        mStorageRef1.getFile(xFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

                Log.e("TamañoArchivo",""+taskSnapshot.getTotalByteCount());
                Log.e("NombreArchivo",""+xFile);

                try {

                    FileOutputStream fos = context.openFileOutput("x.txt", Context.MODE_PRIVATE);
                    fos.write(getStringFromFile(xFile.getAbsolutePath()).getBytes());
                    Log.e("LOG",""+getStringFromFile(xFile.getAbsolutePath()));
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });

        mStorageRef2.getFile(yFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

                Log.e("TamañoArchivo",""+taskSnapshot.getTotalByteCount());
                Log.e("NombreArchivo",""+yFile);

                try {
                    FileOutputStream fos = context.openFileOutput("y.txt", Context.MODE_PRIVATE);
                    fos.write(getStringFromFile(gruposFile.getAbsolutePath()).getBytes());
                    Log.e("LOG2",""+getStringFromFile(gruposFile.getAbsolutePath()));
                    fos.close();
                    fSuccess = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("printStackTrace",""+e.toString());
                    fSuccess = false;
                }
                fSuccess = true;
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                fSuccess=false;
                Log.e("printStackTrace",""+e.toString());
            }

        });
        return fSuccess;
    }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • I suppose the ```getFile```-method is asynchronous, which means that when the return statement is executed, the success-listener is likely not to have been executed yet, which would explain fSuccess not having been updated. – Jan Nash Jan 11 '18 at 13:45

2 Answers2

1

You are returning from method but Asynchronous call is still going on . This is not how it works with callback methods .

You should a Interface to give callback to calling entity .

Read how-to-define-callback

ADM
  • 20,406
  • 11
  • 52
  • 83
0

Seems like you are not updating status of ISuccess on mStorageRef1 success callback. Put a log over there also. And of course you cannot return status from this method as it will not wait for the result(Asynchronous call) and ISuccess will remain unchanged. Probably, it will be better approach to maintain a Status flag (with possible values like STARTED, COMPLETED, IN PROGRESS, ERROR) at class level associated with each Resource and and update the status flag based on addOnSuccessListener callback.

Hope you get what i am trying to say here.

Happy coding!!

RS_Mob
  • 354
  • 1
  • 3
  • 13