用于检查Firebase数据库中是否存在用户的个人资料信息的Firebase Android方法 [英] Firebase Android method to check existence of user's profile info in Firebase database
问题描述
我没有收到任何错误消息,但是我想以不同的方式执行代码.
I don't get any error message but I want to execute code differently.
如果您检查以下Java代码,其运行方式将类似于Tag:1> Tag:2> Tag:4> Tag:5> Tag:3. 我想像Tag:1> Tag:2> Tag:3> Tag:4> Tag:5一样运行它.
If you check following java code, it runs like Tag:1 > Tag:2 > Tag:4 > Tag:5 > Tag:3. I want to run it like Tag:1 > Tag:2 > Tag:3 > Tag:4 > Tag:5.
基本上,我想在开始新活动之前检查用户的个人资料是否存在.我正在寻找只检查Firebase数据库中子代是否存在的方法.我没有更改数据库中的任何数据.因此,它不应该是onDataChange()方法.我认为,旧的Firebase中提供了after()方法来执行相同的操作.有没有可用的方法?
Basically, I want to check user's profile exist or not before starting of a new activity. I am looking for method that just check the existence of child in the Firebase database. I am not changing any data from database. So, It should not be onDataChange() method. I think, there was once() method available in old Firebase to do the same. Is there any method available?
谢谢
Log.i("Tag:","1");
String userId = getUid();
Log.i("Tag:","2");
databaseReference.child("users").child(userId).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserProfile userProfile = dataSnapshot.getValue(UserProfile.class);
Log.i("Tag:","3");
if (userProfile == null){
//do something
} else {
//do something
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Log.i("Tag:","4");
Log.i("Tag:","5");
推荐答案
从您的代码中可以看到:
From what I can see from your code is:
- 您已经具有userProfile的用户ID
- 您正在检查配置文件是否存在于该特定ID上.
好吧,创建一个对象并检查其null或no肯定是一种方法但是 Firebase有一个预定义的方法来检查该特定节点上是否存在数据:
Well, creating an object and checking whether its null or no is definitely one way but Firebase has a pre-defined method for checking whether data exists on that specific node or no:
dataSnapshot.exists()
dataSnapshot.exists()
如果此DataSnapshot包含任何数据,则返回true.稍微 比使用snapshot.val()!== null更有效.
Returns true if this DataSnapshot contains any data. It is slightly more efficient than using snapshot.val() !== null.
这是官方文档中提到的内容.
This is what the official docs mention.
这篇关于用于检查Firebase数据库中是否存在用户的个人资料信息的Firebase Android方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!