我正在尝试Firestore(Android),并正在开发一个简单的一对一聊天应用程序。 除了whereEqualTo()方法外,其他一切都完美无缺 我正在将FirestoreRecyclerAdapter用于messagesActivity
这是我建立查询并将其提供给适配器的代码:
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);
我还添加了几行代码来检查whereEqualTo的内容以及结果:
/////////////////////////////////////////////////////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/////////////////////////////////////////////////////
这就是我在Logcat中得到的东西:
E/MessageAdapter: --------->conversations/AVayc1VKyNMqb83bRqnK8CYLhsi1-K2waGGEE2qYmnAy6UXOiHa7jyvD2<----------
E/MessageAdapter: Snapshot i's empty
这是我正在Firestore数据库中寻找的消息的内容:
conversation: /conversations/AVayc1VKyNMqb83bRqnK8CYLhsi1-K2waGGEE2qYmnAy6UXOiHa7jyvD2
from: /users/K2waGGEE2qYmnAy6UXOiHa7jyvD2
sentAt: July 13, 2018 at 1:45:46 PM UTC+3
text: "TEST MESSAGE"
您可以看到Logcat中的对话ID在数据库中是相同的!