I don't get any error message but I want to execute code differently.
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.
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?
Thank you
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:
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()
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.