0

I am trying to fetch a value from firebase like so:

private String getVCount(String mPath) {
        ArrayList<String> strings = new ArrayList<>();
        FirebaseDatabase.getInstance().getReference("cars").child(mPath).child("maserati")
                .get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull @NotNull Task<DataSnapshot> task) {
                if(task.isSuccessful()){
                    strings.add(task.getResult().getValue().toString());
                }else{
                    strings.add("0");
                }
            }
        });
        Log.d("mString", String.valueOf(strings.size()));
        return strings.get(0);
    }

I double checked the path, and even added an else condition just to make it so that the strings array list has at least one value in it.

The value in the database does exist, and does not exist in some cases--hence the else statement to add a default value of 0. I'm not sure if this is the correct way to do that though.

Additionally, I'm still getting the error: Index: 0 Size: 0--meaning that the strings array list is empty.

Any idea why this may be so?

Thanks.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
CodingChap
  • 1,088
  • 2
  • 10
  • 23
  • Firebase API is asynchronous. So please check the duplicates to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Jan 18 '22 at 07:15

1 Answers1

1

Getting data from Firebase Realtime Database is an async task. So,

Log.d("mString", String.valueOf(strings.size()));
return strings.get(0);

might be getting called before the code inside addOnCompleteListener.

If you want to have your getVCount to have a way to return the result, one way to do this is by creating your own callback like this:

public interface Callback {
    void onCallback(String result);
}
private void getVCount(String mPath, Callback callback) {
        ArrayList<String> strings = new ArrayList<>();
        FirebaseDatabase.getInstance().getReference("cars").child(mPath).child("maserati")
                .get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull @NotNull Task<DataSnapshot> task) {
                if(task.isSuccessful()){
                    strings.add(task.getResult().getValue().toString());
                }else{
                    strings.add("0");
                }
                Log.d("mString", String.valueOf(strings.size()));
                callback.onCallback(strings.get(0));
            }
        });
    }

And call it like this:

getVCount(new Callback() {
    @Override
    public void onCallback(String result) {
        Log.d(TAG, result);
    }
});
danartillaga
  • 1,349
  • 1
  • 8
  • 19