1

I am giving Firestore (Android) a try and working on a simple one to one chat app. Everything is perfect except the whereEqualTo() method I am using FirestoreRecyclerAdapter for the messagesActivity

The is the code where I am building the query and feeding it to the adapter:

        Query query = FirebaseFirestore.getInstance()
            .collection("messages")
            .whereEqualTo("conversation", conversationRef.getPath())
            .orderBy("sentAt");
    FirestoreRecyclerOptions<Message> options = new FirestoreRecyclerOptions.Builder<Message>()
            .setQuery(query, Message.class)
            .build();
    return new MessageAdapter(options);

I also added a few lines to check what's going inside whereEqualTo and what's the result:

/////////////////////////////////////////////////////TESTING/////////////////////////////////////////////////////
    Log.e(TAG, "\n--------->" + conversationRef.getPath() + "<----------\n");//This is to check whit's inserted into whereEqualTo().

    FirebaseFirestore.getInstance()//This is to check whit's the result of the query.
            .collection("messages")
            .whereEqualTo("conversation", conversationRef.getPath())
            .orderBy("sentAt").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot snapshots) {
            if (snapshots != null && !snapshots.isEmpty()) {
                for (DocumentSnapshot snapshot : snapshots) {
                    Message message = snapshot.toObject(Message.class);
                    if (message != null) {
                        Log.e(TAG, message.getSentAt().toString() + "\n");
                        Log.e(TAG, message.getText() + "\n");
                        Log.e(TAG, message.getConversation().getPath() + " " + message.getConversation().getId() + "\n");
                        Log.e(TAG, message.getFrom().getPath() + " " + message.getFrom().getId() + "\n");
                    } else {
                        Log.e(TAG, "Message is empty");
                    }
                }
            } else {
                Log.e(TAG, "Snapshot i's empty");
            }
        }
    });
/////////////////////////////////////////////////////TESTING/////////////////////////////////////////////////////

Here's what I am getting in Logcat:

E/MessageAdapter: --------->conversations/AVayc1VKyNMqb83bRqnK8CYLhsi1-K2waGGEE2qYmnAy6UXOiHa7jyvD2<----------
E/MessageAdapter: Snapshot i's empty

This is the what's inside the message I am looking for in Firestore database:

conversation: /conversations/AVayc1VKyNMqb83bRqnK8CYLhsi1-K2waGGEE2qYmnAy6UXOiHa7jyvD2
from: /users/K2waGGEE2qYmnAy6UXOiHa7jyvD2
sentAt: July 13, 2018 at 1:45:46 PM UTC+3
text: "TEST MESSAGE"

As you can see the conversation Id in Logcat is the same in the the dataBase!!

Amer Alahmar
  • 85
  • 10

0 Answers0