How to get data from a real-time database in Firebase

I used a real-time database with this setting:

->users ->uid ->name ->email ->other info 

If I wanted to save user data, I would use my User class and then set the object in the database as follows:

 //assume variables have already been declared... mFirebaseAuth = FirebaseAuth.getInstance(); mFirebaseUser = mFirebaseAuth.getCurrentUser(); User user = new User(name, email, other values...); mDBRef.child("users").child(mFirebaseUser.getUid()).setValue(user); 

I tried and it works great.

But how can I get these values ​​from the database? For example, as soon as I set the User object as shown above, I can receive the user's email. I do not want to receive email through the input provider. I want to receive email through a database in real time. I tried to work this out for a while, but the documentation doesn't help much. All he shows is to set up listeners to wait for changes in the data. But I do not expect changes, I do not want a listener. I want to directly get the values ​​in the database using the keys in the JSON tree. Is this possible with a real-time database? If so, how can this be done, because either the documentation does not explain it, or I just don’t understand it. If this is not possible, should I use the repository database or something else? Thanks.

0
android firebase firebase-database
source share
1 answer

Firebase uses listeners to receive data. This is just the nature of how it works. Although, you can use one event listener (the equivalent of just capturing data once). It will burn immediately and receive data and will no longer work. Here is a sample code to get the user's current email:

 //Get User ID final String userId = getUid(); //Single event listener mDatabase.child("users").child(userId).addListenerForSingleValueEvent( new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // Get user value User user = dataSnapshot.getValue(User.class); //user.email now has your email value } }); 
+3
source share

All Articles