Home > Net >  Multiple query orderBy clauses Firebase - Java
Multiple query orderBy clauses Firebase - Java

Time:09-06

I am trying to query my Messages table for three things. The first is any messages with that involve the current user logged in. Then, I want to get the user id of the person the current user has a coversation with. And finally, I want to check if any of the messages were seen. But right now my query only runs the first and last queries and not the second one. But I don't know why.

Messages table

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();

DatabaseReference userMessageKeyRef = dbRef.child("Messages").child(firebaseUser.getUid());
userMessageKeyRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot snapshot1 : snapshot.getChildren()) {

            String messageToID = snapshot1.getKey();

            DatabaseReference messageRef = dbRef.child("Messages").child(firebaseUser.getUid()).child(messageToID);
            Query query = messageRef.orderByChild("to").equalTo(firebaseUser.getUid());
            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    long count = dataSnapshot.getChildrenCount();

                    if (dataSnapshot.exists()) {

                        DatabaseReference messageKeyRef = dbRef.child("Messages").child(firebaseUser.getUid()).child(messageToID);
                        Query query2 =  messageKeyRef.orderByChild("isSeen").equalTo(false);

                        query2.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataShot) {

                                if (dataShot.exists() && count > 0) {

                                    //Log.d("TAG2", "count if: "   count2);
                                    messages_text.setText(""   Math.toIntExact(count));
                                    messages_text.setVisibility(View.VISIBLE);
                                } else {

                                    messages_text.setVisibility(View.GONE);
                                }
                            }

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

                            }
                        });

                    } else {

                        messages_text.setVisibility(View.GONE);
                    }

                }

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

                }
            });

            //Log.d("TAG1", "User to: "   messageToID);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
    }
});

CodePudding user response:

Your first addValueEventListener already loads all data for the current user, so there is no need to go back to the database to load that data again. Instead you should check the properties of each message in your application code with something like:

DatabaseReference userMessageKeyRef = dbRef.child("Messages").child(firebaseUser.getUid());
userMessageKeyRef.addValueEventListener(new ValueEventListener() {
    for (DataSnapshot parentSnapshot : snapshot.getChildren()) {
        for (DataSnapshot messageSnapshot : addValueEventListener.getChildren()) {
            String messageToID = messageSnapshot.getKey();
            String toValue = messageSnapshot.child("to").getValue(String.class);
            Boolean isSeenValue = messageSnapshot.child("isSeen").getValue(Boolean.class);

            if (toValue.equals(firebaseUser.getUid()) && isSeenValue == true) {
                ...
            }
        }
    }
});
  • Related