如何在Firestore中使用引用类型? [英] How to work with the reference type in firestore?

查看:32
本文介绍了如何在Firestore中使用引用类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下 Firestore 文档结构:

这是输出

如您所见,在 1:00 PM 下显示的是 12:00 PM 下的 stop .我认为这是由于 asynchronous 请求所致.那么我该如何处理呢?

As you can see the stop which is under 12:00PM is showing under the 1:00PM. I think it is due to asynchronous request. So how I can handle it?

实际上首先我要获取文档,然后获取出发时间的 keys 键,然后再获取该对象的 stops 数组时间,现在再次发送请求以获取站点名称和城市.

Actually first of all I'm getting the document then get the keys of departure which are times then get the stops array of that time, now again send the request to get the stop name and city.

这是我用来获取时间和该时间所有停靠点的代码.

Here is the code which I'm using to fetch the time and all the stops of that time.

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));
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            }
        }
    }
});

推荐答案

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

To solve this, please use the following code:

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 .

Don't use an addSnapshotListener unless you need to get realtime updates.

这篇关于如何在Firestore中使用引用类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆