【问题标题】:How to work with the reference type in firestore?如何使用 Firestore 中的引用类型?
【发布时间】:2018-05-05 15:56:39
【问题描述】:

我有以下Firestore 文档结构:

这是输出

如您所见,12:00PM 下的stop 显示在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));
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            }
        }
    }
});

【问题讨论】:

  • 使用Async任务来处理,我怀疑是从这个
  • 示例可以节省我很多时间。 -_-
  • @D.'s 这是不同的问题。他们只从一个文档中获取幕后,但由于参考,我需要从多个文档中获取。
  • 这对@FrankvanPuffelen 来说是个好问题

标签: java android firebase google-cloud-firestore


【解决方案1】:

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

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

【讨论】:

  • 每个站点都有一个引用,那么如何在不向 firestore 发送新请求的情况下获取站点名称和城市缩写?
  • 您必须向 Firestore 发送新请求。但正如我在此 post 中回答的那样,您可能会考虑更改数据库结构,以便可以基于地图中存在的元素进行查询并更轻松地显示数据。
  • 一切都好吗?我可以帮助您提供有关此问题的其他信息吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多