填充数组,然后调用函数

时间:2020-05-11 14:47:41

标签: ios arrays swift completionhandler

完成获取数据后,我一个接一个地接收到数据项。我想将此项目附加到数组中,然后调用数据,该怎么办?我如何告诉函数等待所有数据追加到数组中-数据数量不同。

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)

1 个答案:

答案 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中的所有情况下

More Info

Dispatch group

DispatchSemaphore