Firestore嵌套查询检索

时间:2018-06-24 16:35:59

标签: ios swift database firebase google-cloud-firestore

我有类似以下代码:

func fetchGroups(completion: ([Group]) -> Void){
    groups.whereField("someField", isEqualTo: "blah").getDocuments { (snapshot, error) in
        if let documents = snapshot?.documents{

            var ret = [Group]()

            for document in documents{
                let data = document.data()
                let v1 = data["V1"] as! String
                let id = data["ID"] as! String

                self.groups.document(id).collection("someCollection").getDocuments(completion: { (snapshot, error) in
                    if let documents = snapshot?.documents{
                        var arry = [String]()
                        for document in documents{
                            let data = document.data()
                            arry.append(data["V2"] as! String)
                        }
                        ret.append(Group(v1, arry))
                    }
                })
            }
            completion(ret)
        }
    }
}

运行此代码时,函数的完成处理程序不包含组数组。经过研究,我发现此行为是嵌套异步检索的结果。如何解决此问题,以使完成处理程序确实包含一组数组?

0 个答案:

没有答案