Java Google Firebase 检查孩子是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37397205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google firebase check if child exists
提问by zomnombom
In my app, I need to check if a given element of my database on firebase has a child with a given name. I hoped it could be done by using something along the lines of:
在我的应用程序中,我需要检查我在 firebase 上的数据库的给定元素是否有一个给定名称的孩子。我希望可以通过使用以下内容来完成:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
if (rootRef.childExists("name")) {
//run some code
}
I searched but I couldn't find anything useful.
我搜索过,但找不到任何有用的东西。
采纳答案by Thomas Bouldin
Edit 2; worth putting on top:I think it is worth mentioning that this is actually downloading all data at this snapshot just to check whether any data exists. You should be mindful here. If the reference is huge (e.g. actuallythe root reference and not a specific child/property) then you should either find a deeper node you can use to check for existence or design your data structure differently so an efficient check is possible.
编辑 2; 值得一提:我认为值得一提的是,这实际上是在此快照处下载所有数据,只是为了检查是否存在任何数据。你应该注意这里。如果引用很大(例如实际上是根引用而不是特定的子/属性),那么您应该找到一个更深的节点,您可以使用它来检查是否存在或以不同的方式设计您的数据结构,以便进行有效的检查。
A database reference is effectively the URL for that data. You want to actually getdata to see whether a child exists. This is why the method you seem to be looking for is on DataSnapshot.
数据库引用实际上是该数据的 URL。您想要实际获取数据以查看子项是否存在。这就是您似乎正在寻找的方法在DataSnapshot 上的原因。
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("name")) {
// run some code
}
}
});
Now, this design pattern feels a bit strange. You're reading the whole database just to see whether "name" exists. You can make this a bit more efficient by listening to rootRef.child("name")
and then just checking whether snapshot.exists()
.
现在,这种设计模式感觉有点奇怪。您正在阅读整个数据库只是为了查看“名称”是否存在。您可以通过聆听rootRef.child("name")
然后检查是否snapshot.exists()
.
If you're trying to do validationhere, and not control flow, you should consider putting this code in your rules.json
.
如果您尝试在此处进行验证,而不是控制流,则应考虑将此代码放入rules.json
.
edit: I originally used the wrong function name (childExists instead of hasChild)
编辑:我最初使用了错误的函数名称(childExists 而不是 hasChild)
回答by Rahul Shah
Try using .childexists
in combination with .equalTo("Your specific name")
尝试.childexists
结合使用.equalTo("Your specific name")
回答by Arjun
UsersRef = FirebaseDatabase.getInstance().getReference();
Users.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("childName")) {
// it exists!
}else{
// does not exist
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
回答by Arsen Nersisyan
Don't do like this
不要这样做
NEVER
绝不
It will take all your data and bring to device
它将获取您的所有数据并传送到设备
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
void onDataChange(DataSnapshot snapshot) {
if (snapshot.hasChild("name")) {
// run some code
}
}
});
Check it by this way. It will return the value of the child if exists, otherwise -> null
通过这种方式检查。如果存在,它将返回孩子的值,否则 -> null
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("childName")
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
void onDataChange(DataSnapshot snapshot) {
if (snapshot.getValue() == null) {
// The child doesn't exist
}
}
});
回答by John yepthomi
Use snapshot.exists() to check if the referenced database entry contains a child , irrespective of the value of the child.
使用 snapshot.exists() 检查引用的数据库条目是否包含 child ,而不管 child 的值。