完成获取数据后,我一个接一个地接收到数据项。我想将此项目附加到数组中,然后调用数据,该怎么办?我如何告诉函数等待所有数据追加到数组中-数据数量不同。
self?.dataModel.fetchExportCSV(id: id, completion: { [weak self] in
switch $0 {
case .success(let data):
var arryData: [exportVO] = []
arryData.append(data)
//this is the fucntion that should be called after all the data are append to the array
createCSV(data: arryData)
答案 0 :(得分:0)
self?.dataModel.fetchExportCSV(id: id, completion: { [weak self] in
let dGroup = DispatchGroup()
switch $0 {
case .success(let data):
dGroup.enter()
var arryData: [exportVO] = []
arryData.append(data)
dGroup.leave()
}
//after switch case
dGroup.notify(queue: .main) {
createCSV(data: arryData)
}
}
将.enter().leave()添加到switch中的所有情况下