1

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?

manfcas
  • 1,933
  • 7
  • 28
  • 47
user3606920
  • 111
  • 1
  • 10
  • 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 Jul 07 '16 at 04:21
  • @FrankvanPuffelen Thanks for reply, I updated the question already. – user3606920 Jul 07 '16 at 04:46
  • 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](http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786). – Frank van Puffelen Jul 07 '16 at 14:24

1 Answers1

2

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) {

        }
    });
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Thanks for reply. Maybe I am not clear enough sorry about that, i've updated the json file, it should be something like this, one uid got different of unique key. – user3606920 Jul 07 '16 at 05:14
  • This is a correct answer but not full answer: use this code and not `ValueEventListener` as you did. But update your `ref` with `limitToFirst(2)` to get only two different uid. Also you should add more parameters to your query ref. – Ami Hollander Jul 07 '16 at 06:18
  • @AmiHollander thanks for reply, but it is not working, I still need both of the child($uid) to get the data which specify by unique key. – user3606920 Jul 07 '16 at 07:13