我试着把所有有“数学”标签的问题都拿出来,但它并没有成功,而是转移到了失败的听众身上。
db.collection("questionCollection")
.orderBy("questionID", Query.Direction.DESCENDING)
.whereArrayContains("tags","maths")
.limit(3)
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (queryDocumentSnapshots.isEmpty()) {
Log.d(TAG, "onSuccess: LIST EMPTY");
return;
} else {
// Convert the whole Query Snapshot to a list
// of objects directly! No need to fetch each
// document.
questionList = queryDocumentSnapshots.toObjects(QuestionBO.class);
if (questionList != null && questionList.size() > 0)
mAdapter.updateQuestions(questionList);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
Toast.makeText(mContext,"Failed",Toast.LENGTH_LONG).show();
}
});
例外情况
FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/project/testingproject-384af/database/firestore/indexes?create_index=EhJxd