Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if firebase query is empty

I want to delete the data from Firebase before unauthorizing. The problem is that mFirebaseRef.unauth() works only if query is not empty. But I need it to work even if query is empty.

final Firebase pushNotificationRef = new Firebase(Constant.FIREBASE_URL_PUSHNOTIFICATIONS);
    final Query queryRef = pushNotificationRef.orderByChild("deviceToken").equalTo(token);
    queryRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.exists()) {
                Log.e("MyTag", dataSnapshot.getKey());
                pushNotificationRef.child(dataSnapshot.getKey()).removeValue();
            }
            mFirebaseRef.unauth();
        }
like image 377
PerSpiKyliaTor Avatar asked Mar 19 '16 09:03

PerSpiKyliaTor


People also ask

How do I check my firebase data usage?

After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime Database.

What is getKey () in firebase?

public String getKey () Returns. The key name for the source location of this snapshot or null if this snapshot points to the database root.

What is DataSnapshot in firebase?

A DataSnapshot is an efficiently-generated immutable copy of the data at a Firebase Location.

Can we retrieve data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.


1 Answers

use this...

if  (dataSnapshot.exists())
    // user found 
else
    // user not found 

demo example

Query query = dbstud.child("users").orderByChild("name").equalTo("XYZ");

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        if(dataSnapshot.exists()) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {


                Toast.makeText(getApplicationContext(), "id = " + snapshot.getKey(), Toast.LENGTH_LONG).show();
            }

        }
        else {
            Toast.makeText(getApplicationContext(), "User Not found ", Toast.LENGTH_LONG).show();


        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});
like image 126
jaigish Avatar answered Sep 21 '22 17:09

jaigish