0

My firebase realtime database structure looks like this:

user id 1
    user id 2
        request: false
        seen: true
        timestamp: 1526021149790
user id 3
    user id 4
        request: true   
        seen: false
        timestamp: 1526021161002

Now i want to sort the recyclerview by timestamp with only items whose request value is false.

Currently i can either sort them by timestamp or request value but cannot do both. My current code is:
...................................................................................................................................................

Query conversationQueryByTimestamp = mConvDatabase.orderByChild("timestamp");
    FirebaseRecyclerOptions<Conv> options =
            new FirebaseRecyclerOptions.Builder<Conv>()
                    .setQuery(conversationQueryByTimestamp, Conv.class)
                    .build();

    firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Conv, FirebaseUserDataViewHolder>(options) {
                @Override
                public FirebaseUserDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.messageitem_layout, parent, false);
                    return new FirebaseUserDataViewHolder(view);
                }

                @Override
                protected void onBindViewHolder(@NonNull final FirebaseUserDataViewHolder holder, final int position, @NonNull final Conv model) {
                    Log.i("firesbaseCheck", "on bind view holder");
                    final String list_user_id = getRef(position).getKey();

                    Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);

                    lastMessageQuery.addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                            String data = dataSnapshot.child("message").getValue().toString();
                            String time = dataSnapshot.child("time").getValue().toString();

                            long lastTime = Long.parseLong(time);

                            holder.setMessage(data, model.isSeen());
                            holder.setTime(getTimeAgo(lastTime));

                        }

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

                        }
                    });

                    mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            final String userName = dataSnapshot.child("name").getValue().toString();
                            String userImage = dataSnapshot.child("image").getValue().toString();

                            holder.setName(userName);
                            holder.setUserImage(userImage, getContext());

                            holder.mView.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {


                                    Intent chatIntent = new Intent(getContext(), MessageSingleActivity.class);
                                    chatIntent.putExtra("user_id", list_user_id);
                                    chatIntent.putExtra("type", "chat");
                                    startActivity(chatIntent);

                                }
                            });


                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

                }
            };

    rvChatList.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.notifyDataSetChanged();
Dee_wab
  • 1,171
  • 1
  • 10
  • 23
Saugat Jonchhen
  • 356
  • 5
  • 16

1 Answers1

0

It's not possible to do this in firebase real-time database. You can only provide filtering or sorting to a single child.

You can achieve this if you use Firebase Firestore. It supports compound queries.

Refer this to know about the advantages of firestore.

Other solutions: (not that efficient)

Instead of getting sorted data based on timestamp, get data whose request value is "false". So that it will not return all the users. It will just return the list of users that you need. Then in your code, you sort them based on timestamp.

Or

You have to maintain another reference which only stores users whose request is false, so you can order them based on timestamp. Delete them from the reference if the value changes to true. you can use cloud function for this purpose.

Jerin A Mathews
  • 8,572
  • 4
  • 26
  • 49