Hi searching a lot but cant find any questions who are related with my topic / or was answered.
Im using Firebase Database with these structure (from firebase official documentation)
// An index to track Ada's memberships
{
"users": {
"alovelace": {
"name": "Ada Lovelace",
// Index Ada's groups in her profile
"groups": {
// the value here doesn't matter, just that the key exists
"techpioneers": true,
"womentechmakers": true
}
},
...
},
"groups": {
"techpioneers": {
"name": "Historical Tech Pioneers",
"members": {
"alovelace": true,
"ghopper": true,
"eclarke": true
}
},
...
}
}
The Docu says: Check if /$uid/groups/$group_id is null
This approach, inverting the data by listing the IDs as keys and setting the value to true, makes checking for a key as simple as reading /users/$uid/groups/$group_id and checking if it is null. The index is faster and a good deal more efficient than querying or scanning the data.
But how can i manage this in Java Code?
With something like this?
mDatabase.child("table/foo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//
i need a simple true or false after this query
if(query) {
user is in Group
... else {
User is not in group
Just or better highlighting
key is my userID
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference memberDatabaseReference = rootRef.child("groups").child(groupName).child("members").child(key);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
Log.d("TAG", "Member does not exist!");
} else {
Log.d("TAG", "Member exists!");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
memberDatabaseReference.addListenerForSingleValueEvent(valueEventListener);