-1

In completion of a fetch data, I receive a data item one by one. I want to append this item in an array and then call data, how can I do that? how I can tell to the function to wait untill all data append to the array - and the number of data is different.

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)
Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
Peter hes
  • 101
  • 6

1 Answers1

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)
      }
    }

add the .enter() .leave() to all the cases in switch

More Info

Dispatch group

DispatchSemaphore

Dilan
  • 2,610
  • 7
  • 23
  • 33