1
votes

Is there any way to get another user's uid from realtime database? I want to get 2 name objects from the database with different uids at the same time.

Json file:

"Users" : {
  "PnZdR1Ily5R5R58FYFVEuWN0Q855" : {
     "-Kwwceeevvvvvttvv" : {
      "name" : "abc"
      }, 
     "-Kccrr325gdfgdfFg" : {
      "name" : "abc"
      }
    }, 
  "MeTsW4SsW6e8hGrsFfVUuNNaQ3cj" : {
     "-Kfvr345345354555" : {
      "name" : "zxc"
      }, 
     "-Kbfhf56464646646" : {
      "name" : "zxc"
      }
  }
}

This is my code:

ValueEventListener postListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                User details = dataSnapshot.getValue(User.class);
                mName = details.name;
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.w(TAG, "getUser:onCancelled", databaseError.toException());
            }
        };
mDatabase.child("Users").child($uid).addValueEventListener(postListener);

This is the part I'm stuck on, how do I get both uids at the same time?

1
I'm having a hard time understanding your question. Can you show some of your actual JSON (as text, which you can export from the Firebase Console) and the code that shows what you're trying to do (and where you are stuck)? - Frank van Puffelen
@FrankvanPuffelen Thanks for reply, I updated the question already. - user3606920
It seems you are looking for a query similar to SQLs WHERE id IN [id1, id2]. Firebase doesn't have such a mechanism, so you'll have to load each child item in turn. This is not significantly slower than loading them in a sql query, because Firebase pipelines the requests. - Frank van Puffelen

1 Answers

2
votes

If you want to retrieve random key then you should use addChildEventValueListerner because it gives u child of the Users node one by one.

 FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
 DatabaseReference mref = firebaseDatabase.getReference();
 mref.child("Users").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
              if (dataSnapshot.hasChildren()) {
                  User details = dataSnapshot.getValue(User.class);
              }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });