0

CODE

private void uploadfirebase() {

    hashMap.put("Images", "nnn");


    hashMap.put("Title", "tit");

    String childer = System.currentTimeMillis() + FirebaseAuth.getInstance().getCurrentUser().getUid();

    databaseReference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(UploadImages.this, "Done", Toast.LENGTH_SHORT).show();

        }
    });

    ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Uploading");
    progressDialog.show();

    for (int i = 0; i < imagePath.size(); i++) {


        Uri uri = Uri.fromFile(new File(imagePath.get(i)));

        String currenttime = System.currentTimeMillis() + FirebaseAuth.getInstance().getCurrentUser().getUid();


        int finalI = i;
        storageReference.child(currenttime).putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {


                taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {

                        String url = String.valueOf(uri);
                        List<String> urlList1 = Collections.singletonList(url);
                        addToFirebaseUris.add(url);
                        Toast.makeText(UploadImages.this, "addtoFirebaseList size : " + addToFirebaseUris.size(), Toast.LENGTH_SHORT).show();
                        Log.d("list", "addtoFirebaseList size : " + addToFirebaseUris.size());


                        //  urls = Collections.singletonList(url);


                    }


                });


            }


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


                progressDialog.dismiss();


                Toast.makeText(UploadImages.this, e.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });


    }

    Log.d("list", "method called");
    Toast.makeText(this, "Method callede after loop", Toast.LENGTH_SHORT).show();
    try {
        addtodatabase(addToFirebaseUris);
    } catch (Exception e) {
        Toast.makeText(UploadImages.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }


}

PROBLEM

This "Log.d("list","addtoFirebaseList size : "+addToFirebaseUris.size());" statement should be executed before the "Log.d("list","method called");" statment. But the reverse is happening in my code. The expected outcome should be that the "Log.d("list","addtoFirebaseList size : "+addToFirebaseUris.size());" should be executed as long as the loop is active and after that other statements after the loop should be executed.

  • No. You started a thread. And then your method is called. Now you sit down and wait until the thread finishes. Only then onSucces() or onFailure() are triggered. – blackapps Sep 22 '21 at 08:43
  • That is happening because Firebase API is asynchronous. So you need to wait until the operation completes. So please check the duplicate answer. – Alex Mamo Sep 22 '21 at 08:43

0 Answers0