0

I have a question regarding Firebase Realtime database.

I'm trying to do a bookmark option in my program, which allows the user to store his/her's favourite pages, however whenever I try to retrieve data from my firebase database, the data is restored after the method returns a value.

public static boolean checkIfBookmarked(final String title){
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    final DatabaseReference userBookmarks = FirebaseDatabase.getInstance().getReference().child("users")
            .child(user.getUid()).child("bookmarks");
    final boolean[] exists = new boolean[1];
    userBookmarks.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            exists[0] = dataSnapshot.child(title).exists() ;
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return exists[0];
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

1

Firebase use callback methods to get the data from the server, In your case the return statement will be executed before the callback come from the Firbase. You can try to pass a callback method to your function and execute that when the callback from Firebase is triggered.

public static void checkIfBookmarked(final String title, callbackFunction){
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    final DatabaseReference userBookmarks = FirebaseDatabase.getInstance().getReference().child("users")
            .child(user.getUid()).child("bookmarks");
    final boolean[] exists = new boolean[1];
    userBookmarks.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            exists[0] = dataSnapshot.child(title).exists() ;
            //execute your callback function here
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return;
}

Check this to see how to pass a function as parameter. Passing function as a parameter in java

An alternative would be to move your code into OnDataChange method

Mario Vasile
  • 96
  • 3
  • 3
1

You cannot return something now that hasn't been loaded yet. With other words, you cannot simply return the first element of your array exists[0], outside the onDataChange() method because it will always be null due the asynchronous behaviour of this method. This means that by the time you are trying to use that result outside that method, the data hasn't finished loading yet from the database and that's why is not accessible.

A quick solve for this problem would be to use exists[0] only inside the onDataChange() method, or if you want to use it outside, I recommend you dive into the asynchronous world and see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193