Java Firebase云Firestore遵循参考

Java Firebase云Firestore遵循参考,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我得到了一个带有以下代码的queryDocumentSnapshot: mFireStore = FirbaseFirestore.getInstance(); mFireStore.collection("myCollection").addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@Nullable Quer

我得到了一个带有以下代码的
queryDocumentSnapshot

mFireStore = FirbaseFirestore.getInstance();
mFireStore.collection("myCollection").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
                    if (doc.getType() == DocumentChange.Type.ADDED) {
                        // Set some data from the doc.getDocument here

mFireStore.collection("myCollection").document(doc.getDocument().getId()).collection("subCollection").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                    @Override
                                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                                        if (queryDocumentSnapshots.isEmpty()){
                                            // not found
                                        } else{

                                            List<DocumentSnapshot> hosts = queryDocumentSnapshots.getDocuments();

                                            for (int i = 0; i < hosts.size(); i++){
                                                // Each hosts DocumentSnapshot has a "host" field which contains a reference to a document
                                                // I'd like to follow this reference and get that document
                                            }
                                        }
                                    }
                                });
mFireStore=FirbaseFirestore.getInstance();
mFireStore.collection(“myCollection”).addSnapshotListener(新的EventListener(){
@凌驾
public void OneEvent(@Nullable QuerySnapshot queryDocumentSnapshots,@Nullable FirebaseFirestoreException e){
对于(DocumentChange文档:queryDocumentSnapshots.getDocumentChanges()){
if(doc.getType()==DocumentChange.Type.ADDED){
//在这里设置doc.getDocument中的一些数据
mFireStore.collection(“myCollection”).document(doc.getDocument().getId()).collection(“subCollection”).get().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时公共无效(QuerySnapshot QueryDocumentSnapshot){
if(queryDocumentSnapshots.isEmpty()){
//找不到
}否则{
List hosts=queryDocumentSnapshots.getDocuments();
对于(int i=0;i

我的问题是,一旦我得到了主机
文档快照的列表,我想跟随他们的“主机”字段,其中包含对文档的引用。我希望遵循此引用并在另一端获取文档,但不知道如何获取。如果您注意到我的逻辑存在任何其他问题,请随意指出,因为我对Cloud Firestore非常陌生。

要解决此问题,您可以简单地迭代
主机
列表。如果您的
>主机
字段将引用保存为
字符串
,然后请使用以下代码行:

for (DocumentSnapshot ds : hosts) {
    String host = ds.getString("host");
    Log.d("TAG", host);
}
如果您的
主机
字段将引用保存为
文档引用
,请使用以下代码行:

DocumentReference host = ds.getDocumentReference("host");

将这些主机ID存储在一个数组中,然后开始侦听每个ID如果要做到这一点,您需要执行
nestedquerys
,因为您应该对“跟踪”用户内的每个文档执行单个查询。另一种方法是执行云功能,将帖子从该用户复制到“提要”对于跟踪它的用户,这样您将能够始终查询提要,并避免嵌套查询一切正常吗?您尝试过我上面的解决方案吗?