Skip to content Skip to sidebar Skip to footer

How To Make Sure Query Is Not Empty

i have an application with many types of users like normalUser,PremiumUser and in order to define that i use their unique UID in firebasedatabase to each one in their category prem

Solution 1:

Your query will never be null, because you created it just before that. There is no way to know how many results a query has without attaching a listener.

So the closest you can get with your current data structure is to nest the queries:

Query q = FirebaseDatabase.getInstance().getReference().child("users").child("normal").equalTo(auth.getCurrentUser().getUid());
Query q2 = FirebaseDatabase.getInstance().getReference().child("users").child("premium").equalTo(auth.getCurrentUser().getUid());
q.addValueEventListener(new ValueEventListener() {
    publicvoidonDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            startActivity(new Intent(Login.this, Normal.class));
        }
        else {
            q2.addValueEventListener(new ValueEventListener() {
                publicvoidonDataChange(DataSnapshot dataSnapshot) {
                    startActivity(new Intent(Login.this, Premium.class));
                }
                publicvoidonCancelled(DatabaseError databaseError) {
                    throw databaseError.toException();
                }
            });
        }
    }
    publicvoidonCancelled(DatabaseError databaseError) {
       throw databaseError.toException();
    }
});

A more direct way to make this work is to change your data structure so that is also contains the payment status for each user. So also store:

user_payment_status
  uid1: "normal"
  uid2: "premium"

Then you can simply look up the status for the current user with a single value listener. You'll keep your current list around, since you likely also want to show a list of all premium users somewhere in the app.

Duplicating data in this way is quite common in NoSQL databases.

Solution 2:

i fixed it by adding to each user a uid then changed

Queryq= FirebaseDatabase.getInstance().getReference().child("users").child("normal").equalTo(auth.getCurrentUser().getUid());
 Queryq2= FirebaseDatabase.getInstance().getReference().child("users").child("premium").equalTo(auth.getCurrentUser().getUid());

to the following

Queryq= FirebaseDatabase.getInstance().getReference().child("users").child("normal").orderByChild("uid").equalTo(auth.getCurrentUser().getUid());
 Queryq2= FirebaseDatabase.getInstance().getReference().child("users").child("premium").orderByChild("uid").equalTo(auth.getCurrentUser().getUid());

i still dont know why the first way is wrong

Post a Comment for "How To Make Sure Query Is Not Empty"