如何在firestore中使用引用类型?

时间:2018-05-05 15:56:40

标签: java android firebase google-cloud-firestore

我有以下stop文档结构:

enter image description here

这是输出

enter image description here

正如您所见,12:00PM下的1:00PM显示在asynchronous下。我认为这是由于keys请求。那我该怎么办呢?

实际上,首先我得到文档,然后得到times离开的stops然后得到那个时间的db.collection(DBMeta.COLLECTION_ROUTE) .document(routeIntent.getId()) .addSnapshotListener(new EventListener<DocumentSnapshot>() { @Override public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) { Route route = null; if(documentSnapshot!=null) route = documentSnapshot.toObject(Route.class); if(route != null){ if(route.getDeparture() != null){ String[] departures = route.getDeparture().keySet().toArray(new String[route.getDeparture().keySet().size()]); for(String time : departures){ Object object = route.getDeparture().get(time).get(DBMeta.DOCUMENT_ROUTE_STOPS); ArrayList<DocumentReference> stops = (ArrayList<DocumentReference>)object; departureLayout.addView(timeTextView(time)); for(DocumentReference stop : stops){ stop.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { final String bussStop = documentSnapshot.getString(DBMeta.DOCUMENT_STOP_NAME); DocumentReference doc = documentSnapshot.getDocumentReference(DBMeta.DOCUMENT_STOP_CITY); if(doc != null){ doc.get() .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { String cityAbbrev = documentSnapshot.getString(DBMeta.DOCUMENT_CITY_ABBREV); departureLayout.addView(stopTextView(bussStop + " - " + cityAbbrev)); } }); } } }); } } } } } }); 数组,现在再次发送请求获取停止名称和城市。

这是我用来获取时间和所有时间段的代码。

{{1}}

1 个答案:

答案 0 :(得分:2)

要解决此问题,请使用以下代码:

db.collection(DBMeta.COLLECTION_ROUTE).document(routeIntent.getId()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();
                Map<String, Object> departureMap = (Map<String, Object>) map.get("departure");
                for (Map.Entry<String, Object> entry : departureMap.entrySet()) {
                    Log.d(TAG, entry.getKey());
                    Map<String, Object> innerMap = (Map<String, Object>) entry.getValue();
                    for (Map.Entry<String, Object> e : innerMap.entrySet()) {
                        Log.d(TAG, e.getValue().toString());
                    }
                }
            }
        }
    }
});

输出将是:

12:00 PM
ByPass - GRW
1:00 PM
Jail Chowk - GRT

除非您需要获得实时更新,否则不要使用addSnapshotListener