我正在Android应用中使用Cloud Firestore。这是一个测验应用程序,我从Firestore中随机获取文档。当互联网连接良好时,该应用程序可以正常运行。当网络断开连接,然后又重新连接时,我无法阅读文档。调试时,发现我的get()方法根本没有执行。
Iterator iterator = randomIds.iterator();
while (iterator.hasNext()) {
String documentId = (String) iterator.next();
DocumentReference documentReference = db.collection(categoryName).document(documentId);
if (documentReference!=null) {
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
System.out.println("Task successful");
DocumentSnapshot document = task.getResult();
questionDetailsObj = new QuestionDetails();
questionDetailsObj = document.toObject(QuestionDetails.class);
if (questionDetailsObj != null) {
System.out.println("Question details: " + questionDetailsObj.getQuestion_text());
// Adding the questions to a list
questionsList.add(questionDetailsObj);
}
} else {
Log.d("MainActivity", "get() failed with " + task.getException());
}
}
});
}
}
我想检索10个文档。有时,很少能成功检索到文档,而对于其他文档,我会得到例外
get() failed with com.google.firebase.firestore.FirebaseFirestoreException:
Failed to get document because the client is offline.
我不明白为什么有些文档会成功提交而有些文档却无法检索。请帮助我了解是否需要更改任何代码。