Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何同时获取文档数据和参考数据?(Firestore,Swift)_Swift_Firebase_Google Cloud Firestore_Swiftui - Fatal编程技术网

如何同时获取文档数据和参考数据?(Firestore,Swift)

如何同时获取文档数据和参考数据?(Firestore,Swift),swift,firebase,google-cloud-firestore,swiftui,Swift,Firebase,Google Cloud Firestore,Swiftui,我试图同时获取文档数据和引用数据,并将数据放入由字符串数组组成的自定义结构中 当我运行下面的代码时,只追加DocumentReference类型“item1_uu”和“item2_uuu” 集合“部分”有几个文档。每个文档有2个文档引用。 我可以得到正确的文档引用,但我不能在同一个函数中读取它的数据 如何从Firestore获取这两个数据 func getall_sec(top: String, mid: String){ref.collection("top_hierarch

我试图同时获取文档数据和引用数据,并将数据放入由字符串数组组成的自定义结构中

当我运行下面的代码时,只追加DocumentReference类型“item1_uu”和“item2_uuu”

集合“部分”有几个文档。每个文档有2个文档引用。 我可以得到正确的文档引用,但我不能在同一个函数中读取它的数据

如何从Firestore获取这两个数据

    func getall_sec(top: String, mid: String){ref.collection("top_hierarchy").document(top).collection("mid_hierarchy").document(mid).collection("section").addSnapshotListener(){ (snap, err) in
        guard let docs = snap else {
            self.nosecs = true
            return
        }
        
        if docs.documentChanges.isEmpty{
            self.nosecs = true
            return
        }
        docs.documentChanges.forEach { (doc) in
         
            if doc.type == .added{

                let item1_ = doc.document.data()["item1"] as! DocumentReference
                let item2_ = doc.document.data()["item2"] as! DocumentReference


                item2_.getDocument(){ (querySnapshot, err) in
                    if let err = err {
                        print("Error getting documents: \(err)")
                    } else {
                        self.item2_name = querySnapshot?.data()?["low"] as! String
                        self.item2_ImageName = querySnapshot?.data()?["ImageName"] as! String
                    }
                }
                item1_.getDocument(){ (querySnapshot, err) in
                    if let err = err {
                        print("Error getting documents: \(err)")
                    } else {
                        self.item1_name = querySnapshot?.data()?["low"] as! String
                        self.item1_ImageName = querySnapshot?.data()?["ImageName"] as! String
                    }
                }

                self.sections.append(SectionModel(id: doc.document.documentID, item_name1: self.item1_name, item_ImageName1: self.item1_ImageName, item_name2: self.item2_name, item_ImageName2: self.item2_ImageName))

            }
        }
    }
}

主要问题是getDocument是异步的,self.sections.append代码将在两个get函数之后的闭包中的代码之前执行

item2_.getDocument(){ (querySnapshot2, err2) in
   //this code will execute at some point
}
item1_.getDocument(){ (querySnapshot1, err1) in
   //this code will execute at some point
}

//this code will execute before the code in the two above getDocument closures.
self.sections.append(SectionModel(id: doc.document.documentID...
一种解决方案是嵌套调用,以便它们按顺序执行(这不是最佳解决方案,但演示了这一概念)


谢谢Jay,你的反馈真的很有效!
item2_.getDocument(){ (querySnapshot, err) in
   item1_.getDocument(){ (querySnapshot, err) in
      self.sections.append(SectionModel(id: doc.document.documentID...
   }
}