Firebase Realtime Database, load multiple users with one query

Viewed 146

I'm not a database guru, but I remember back then when working with mysql, there was a way to load multiple users with one query, by putting their UID's in a json. Is there a way to do the same with the Firebase Realtime Database (Android/Java)? Thanks in advance!

1 Answers

In MySQL, if you want to load all users you can type this query:

SELECT * FROM tablename; 

Otherwise, if you want to take a specified group of users you can type:

SELECT FROM tablename WHERE condition 

In condition field you can put ID, username, or whatever you want

In Firebase is quite different but still, easy. I recommend you the official documentation. I will mention some lines from the page in question.

Firstly, you have to get the reference of what you want retrieve

final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/fireblog/posts");

You can now use a listener to read data from reference.

ref.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    Post post = dataSnapshot.getValue(Post.class);
    System.out.println(post);
  }

Note that the listener function is called anytime new data is added to your database reference, and you don't need to write any extra code to make this happen.

Related