0

Firebase Strucutre

I am trying to retrieve Messages/child sorted on the base of time. I have access to Messages/child. I am unable to find any useful solution. Please help me figure this out.

My current code is:

FirebaseDatabase.getInstance()
    .getReference()
    .child("Messages")
    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
    .addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(@NonNull DataSnapshot dataSnapshot,  @Nullable, String s) {

              Log.d("children",dataSnapshot.getKey());
              users_list.add(dataSnapshot.getKey());

          }
barbsan
  • 3,418
  • 11
  • 21
  • 28

2 Answers2

0

you can do this way :-

mChatReference = FirebaseDatabase.getInstance().getReference("Messages")
                .child(FirebaseAuth.getInstance().getCurrentUser().getUid());
        Query query = mChatReference.orderByChild("time");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   Log.d("children",dataSnapshot.getKey());
                users_list.add(dataSnapshot.getKey());            
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
Sandeep Malik
  • 1,972
  • 1
  • 8
  • 17
  • `Query query = FirebaseDatabase.getInstance().getReference().child(FirebaseAuth.getInstance().getCurrentUser().getUid()) .orderByChild("time");` when I tried using this, It only returns top level key which is `Messages/key` –  May 13 '19 at 08:13
  • i am only giving the hint to solve your problem add the childs and do your stuff – Sandeep Malik May 13 '19 at 08:38
  • it is fully working but its your bad luck ki tum itna sa kam nahi kr pa rhi ho – Sandeep Malik May 13 '19 at 08:39
  • I need the data in order with reference to time but it isn't according to my requirements. It is retrieving the data in the existing order. –  May 13 '19 at 08:41
  • ok then keep trying to find the accurate answer to solve your problem but the fact is that all the peoples can give you hint and you have to do it by your own – Sandeep Malik May 13 '19 at 08:43
  • just add child in reference and run the code it will work properly – Sandeep Malik May 13 '19 at 08:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193240/discussion-between-sandeep-malik-and-nancy). – Sandeep Malik May 13 '19 at 08:58
0

As far as I can see you have a data structure:

Messages: {
  uid1: {
    uid2: {
      messageId1: ...
      messageId2: ...
      messageId3: ...
    }
  }
}

You attach a ChildEventListener to /Messages/uid1, which means that your onChildAdded gets called for user second-level UID from your JSON. To get to the individual messages, you still need to loop over the child nodes of the DataSnapshot:

FirebaseDatabase.getInstance()
    .getReference()
    .child("Messages")
    .child(FirebaseAuth.getInstance().getCurrentUser().getUid())
    .addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(@NonNull DataSnapshot dataSnapshot,  @Nullable, String s) {
              for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
                  Log.d("message key: ", messageSnapshot.getKey());
                  Log.d("message time: ", messageSnapshot.child("time").getValue(String.class));
              }
          }

A few things to note:

  • As said, this code loops over the children of the DataSnapshot to get to the individual messages.
  • Consider storing the data as chat rooms where the key of each chat room is based on the UID of its participants as explained here: Best way to manage Chat channels in Firebase
  • You will need to add your messages to a list, and sort them client-side.
  • You're storing the time as a display string, which makes it harder to sort them. Consider storing them in a format that is easier to sort, such as "20190513T073306" or a timestamp such as 1557758003677.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have changed the timestamp. How am i supposed to sort them in a list? What I wanna do is to bring the person who has the most recent message on the top of the list of users. is this possible by the approach I'm using/? –  May 13 '19 at 16:33
  • I assume that you're adding all messages to a list in a loop, like I've shown in my answer. In that case you can [sort the list](https://stackoverflow.com/questions/16252269/how-to-sort-an-arraylist), and then pass it to your adapter. – Frank van Puffelen May 13 '19 at 18:41