您好好搜索,但无法找到与我的主题相关的任何问题/或已被回答。
我正在使用具有这些结构的Firebase数据库(来自firebase官方文档)
// 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
}
},
...
}
}
Docu说:检查/ $ uid / groups / $ group_id是否为空
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.
但我如何在Java Code中管理它?
有这样的东西吗?
mDatabase.child("table/foo").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//
在此查询后我需要一个简单的true或false
if(query) {
user is in Group
... else {
User is not in group
正好或更好地突出显示
key是我的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);
答案 0 :(得分:2)
您需要在exists()
对象上使用dataSnapshot
方法。因此,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference memberDatabaseReference = rootRef.child("groups").child(groupName).child("members").child(memberName);
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);
其中mamberName
是您要验证的成员的名称。通过这种方式,您可以验证成员的存在。
希望它有所帮助。
答案 1 :(得分:0)
在您的firebase安全性中创建一个节点,如下所示
{
"rules":{
"checkIfMemberIsInGroup":{
"$groupId":{
"$memberId":{
".read":false,
".write":"auth != null && !newData.exists() &&
root.child('groups').child($groupId).child('members').child($memberId).exists()"
}
}
}
}
然后检查用户是否是轻量级查询的组的一部分,并让服务器占用带宽..
FirebaseDatabase.getInstance().getReference().child("checkIfMemberIsInGroup")
.child(groupName)
.child(memberName)
.setValue(null, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null && databaseError.getCode() == DatabaseError.PERMISSION_DENIED) {
// user is not member of group
} else {
// user is member of group
}
}
});