如何检查firebase查询是否为空

时间:2016-03-19 09:20:40

标签: android firebase firebase-realtime-database

我想在取消授权之前从Firebase删除数据。问题是mFirebaseRef.unauth()仅在查询不为空时才有效。但即使查询为空,我也需要它才能工作。

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();
        }

1 个答案:

答案 0 :(得分:5)

使用此...

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

演示示例

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();
    }
});
相关问题