上一行完成后,如何运行一组代码



我试着调试了下面的每一行代码。在database.collection("users")区段中到达.get().addOnSuccessListener(documentSnapshot ->之后。代码database.collection("categories")和下一个代码被执行,直到database.collection("users")部分的r[0] = Integer.parseInt(documentSnapshot.getString("LastQuestion")));代码被执行结束。

我希望首先执行r[0] = Integer.parseInt(documentSnapshot.getString("LastQuestion")));,这样.whereGreaterThan("index", r[0])就不会在r[0]null的情况下运行

final Integer[] r = new Integer[1];
r[0] = 1;
database.collection("users")
.document("0mu8LcLm8aREn14Qa13LvxTJv9D3")//TODO user hardcoded
.collection(catId)
.document(topicId)
.get().addOnSuccessListener(documentSnapshot ->
r[0] = Integer.parseInt(documentSnapshot.getString("LastQuestion")));
database.collection("categories")
.document(catId)
.collection(topicId)
.orderBy("index", Query.Direction.ASCENDING)
.whereGreaterThan("index", r[0])
.get().addOnSuccessListener(queryDocumentSnapshots -> {
for (DocumentSnapshot snapshot : queryDocumentSnapshots) {
Question question = snapshot.toObject(Question.class);
question.setUId(snapshot.getId());
questions.add(question);
}
setNextQuestion();
});

在这种情况下,您必须在Success监听器中进行第二个查询:

database.collection("users")
*/ ... */
.get().addOnSuccessListener(documentSnapshot -> {
int a = Integer.parseInt(documentSnapshot.getString("LastQuestion")));
database.collection("categories")
/* ... */
.whereGreaterThan("index", a)
/* ... */
.get().addOnSuccessListener(queryDocumentSnapshots -> {
for (DocumentSnapshot snapshot : queryDocumentSnapshots) {
/* ... */
}
}
}

最新更新