【问题标题】:how to change from firestore to firebase realtime database如何从 firestore 更改为 firebase 实时数据库
【发布时间】:2019-10-12 00:43:37
【问题描述】:

您好,我正在将 Firestore 转换为 FirebaseDatabase,我能够完成大部分转换,除了我遇到的这个转换。我被困在 .getdocuments 和 Documentchanges 上。我认为.getdocuments 是.getchildren?我不知道实时火力库中 Documentchanges 的等价物是什么。请帮忙。提前谢谢你!

这是我要转换的代码

 firestore.collection("Users")
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                    if (!queryDocumentSnapshots.getDocuments().isEmpty()) {

                        for (final DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                            if (doc.getType() == DocumentChange.Type.ADDED) {

                                if (!doc.getDocument().getId().equals(mAuth.getCurrentUser().getUid())) {
                                    Friends friends = doc.getDocument().toObject(Friends.class).withId(doc.getDocument().getString("id"));
                                    usersList.add(friends);
                                    usersAdapter.notifyDataSetChanged();
                                    refreshLayout.setRefreshing(false);
                                }

                            }
                        }

                        if(usersList.isEmpty()){
                            refreshLayout.setRefreshing(false);
                            mView.findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                        }

                    }else{
                        refreshLayout.setRefreshing(false);
                        mView.findViewById(R.id.default_item).setVisibility(View.VISIBLE);
                    }

                }
            })

【问题讨论】:

    标签: android firebase-realtime-database google-cloud-firestore


    【解决方案1】:

    实时数据库中没有直接的等价物。

    documentChanges 的目的是了解一组查询结果与上次获得结果相比发生了哪些变化。这仅适用于您使用侦听器接收实时更新(您在这里没有这样做)的情况。如果您想知道 DataSnapshot 相对于您在侦听器中获得的先前 DataSnapshot 的变化,您必须自己弄清楚。

    但这有点离题了。您的 Firestore 代码在此处不必要地使用文档更改来从 单个 不实时更新的结果集中获取文档列表。如果您没有使用实时侦听器,那么 documentChanges 实际上并不是真正适合使用的东西。每个文档都将显示为“已添加”。对于一组查询结果,您应该只迭代 queryDocumentSnapshots.getDocuments() 返回的列表。

    对于实时数据库 DataSnapshot,getDocuments() 的等价物只是 getChildren(),这将为您提供一种迭代子节点的方法。

    【讨论】:

      猜你喜欢
      • 2021-01-09
      • 2021-10-19
      • 2021-08-24
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      相关资源
      最近更新 更多