[英]Firestore Query Problem: Where is I am mistaken?
我正在嘗試為博物館構建一個移動應用程序。 我正試圖在 Firestore 中找到一個 object,它有一個像 integer 1,2,3 這樣的字段......我為此寫了一個查詢,但它總是以失敗告終。 你能看出我錯了嗎? 謝謝。 我現在更新了有問題的查詢。 我猜問題出在內部 class 變量中。 當我嘗試在查詢中使用最終變量時它不會成功。
final int index = Integer.parseInt(inputString) - 1;
CollectionReference mapLocation = firebaseFirestore.collection("mapLocations");
Query query = mapLocation.whereEqualTo("artifactID", inputString);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for(QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()){
Map<String,Object> currentArtifact = new HashMap<>();
currentArtifact = queryDocumentSnapshot.getData();
int cX = Integer.parseInt(currentArtifact.get("X").toString());
int cY = Integer.parseInt(currentArtifact.get("Y").toString());
artifacts[index].setBackgroundColor(artifacts[index].getContext().getResources().getColor(R.color.Green));
artifacts[index].setX(cX);
artifacts[index].setY(cY);
documentReferenceUser.update("path",artPath);
}
}
else{
Toast.makeText(context,"Failure",Toast.LENGTH_LONG);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
return;
}
});
int index = Integer.parseInt(inputString) - 1;
final int tempIndex = index;
final String tempStr = inputString;
Task<QuerySnapshot> documentReference = firebaseFirestore.collection("mapLocations")
.whereEqualTo("artifactID",index)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
boolean flag = task.isSuccessful();
Log.d("flag",String.valueOf(flag));
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
Map<String, Object> currArtifact = new HashMap<>();
currArtifact = document.getData();
int coordinateX = Integer.parseInt(currArtifact.get("X").toString()) ;
int coordinateY = Integer.parseInt(currArtifact.get("Y").toString()) ;
artifacts.get(tempIndex).setBackgroundColor(artifacts.get(tempIndex).getContext().getResources().getColor(R.color.Green));
playerButton.setX(coordinateX);
playerButton.setY(coordinateY);
artPath.add(tempStr);
documentReferenceUser.update("path",artPath);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
所以這個問題的答案是你不能使用或傳遞尚未加載的數據。 對 Firestore 的調用是異步調用,這意味着它啟動了一個進程來對 Firestore 進行查詢,一旦完成,它就會執行onComplete偵聽器。 這就是您遇到失敗的原因,因為您無法在調用數據庫后立即獲取數據。
在這里,您可以做的是,擁有一個采用ArrayList
並處理數據的數據處理程序方法。 檢索或獲取所有數據后,將從onComplete
偵聽器調用此方法。
例如:
Task<QuerySnapshot> documentReference = firebaseFirestore.collection("mapLocations")
.whereEqualTo("artifactID",index)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
boolean flag = task.isSuccessful();
Log.d("flag",String.valueOf(flag));
if (task.isSuccessful()) {
List<Data> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
list.add(new Data(document.getData()));
}
metho1(list);
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
metho1(list){
for (int i=0; i< list.length(); i++){
currArtifact = list[i].getData();
int coordinateX = Integer.parseInt(currArtifact.get("X").toString()) ;
int coordinateY = Integer.parseInt(currArtifact.get("Y").toString()) ;
artifacts.get(tempIndex).setBackgroundColor(artifacts.get(tempIndex).getContext().getResources().getColor(R.color.Green));
playerButton.setX(coordinateX);
playerButton.setY(coordinateY);
artPath.add(tempStr);
documentReferenceUser.update("path",artPath);
}
}
請也看看這個類似的帖子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.