我有类似以下代码:
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)
}
}
}
运行此代码时,函数的完成处理程序不包含组数组。经过研究,我发现此行为是嵌套异步检索的结果。如何解决此问题,以使完成处理程序确实包含一组数组?