Skip to content Skip to sidebar Skip to footer

How To Get A Value Once Of A Firebase Database (android)

I just started using firebase and I'm amazed of how simple are complex things, and how complex simple things can be. I need to check if a user exists in my Firebase database, and i

Solution 1:

You say: I can't do this with the overriden method onDataChanged() since the data does not change when I query for this value

The guide for how to Retrieve Data explains that:

Firebase data is retrieved by attaching an asynchronous listener to a FirebaseDatabase reference. The listener is triggered once for the initial state of the data and again anytime the data changes.

So when you attach a listener to a location, onDataChanged() fires and gives you the current value.

In the section titled Read Data Once, the guide states:

In some cases you may want a callback to be called once and then immediately removed, such as when initializing a UI element that you don't expect to change. You can use the addListenerForSingleValueEvent() method to simplify this scenario: it triggers once and then does not trigger again.

Solution 2:

Use as below :

friend_found.addListenerForSingleValueEvent(newValueEventListener() {
            @OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {

                 String value = dataSnapshot.getValue(String.class);
            }

            @OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
                Log.e(TAG,"Error while reading data");
            }
        });

Solution 3:

For a database reference object, the same way one can add an event listener, it can also be removed, using removeEventListener.

Instead of creating an anonymous object like this

    friend_found.addListenerForSingleValueEvent(newValueEventListener() {
            @OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {

                    String value = dataSnapshot.getValue(String.class);
            }

            @OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
                    Log.e(TAG,"Error while reading data");
            }
    });

you can create a named object of ValueEventListener and remove it from the database reference object using removeEventListener, at the end of the onDataChange method

ValueEventListener valueEventListener = newValueEventListener() {
            @OverridepublicvoidonDataChange(@NonNull DataSnapshot dataSnapshot) {

                    String value = dataSnapshot.getValue(String.class);

                    friend_found.removeEventListener(valueEventListener);
            }

            @OverridepublicvoidonCancelled(@NonNull DatabaseError databaseError) {
                    Log.e(TAG,"Error while reading data");
            }
    });

    friend_found.addValueEventListener(valueEventListener);

The code inside onDataChange method gets executed only once as the ValueEventListener object is removed as soon as the last line of the method gets executed.

Post a Comment for "How To Get A Value Once Of A Firebase Database (android)"