5

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();
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
PerSpiKyliaTor
  • 91
  • 1
  • 1
  • 7
  • 1
    The `onChildAdded()` method will only be called **if** a child is present, so you cannot use it to detect when no child is present. See this question for how to detect that: http://stackoverflow.com/questions/34460779/what-happens-if-a-firebase-url-doesnot-exist-and-we-try-to-add-a-listener-to-it/34463972#34463972 – Frank van Puffelen Mar 19 '16 at 14:19

1 Answers1

8

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();
    }
});
Max Play
  • 3,717
  • 1
  • 22
  • 39
jaigish
  • 157
  • 2
  • 4