Answers for "DispatchGroup example swift"

3

DispatchGroup example swift

class DispatchGroupExample: UIViewController {
  private let dispatchGroup = DispatchGroup()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    fetchData(url: xxx)
    fetchData(url: yyy)
    
    // 3: notify job done
    dispatchGroup.notify(queue: .main) {
      self.goToNext()
    }
  }
  
  private func fetchData(url: URL) {
    // 1: enter group
    dispatchGroup.enter()
    
    URLSession.shared.dataTask(with: url) { 
      [weak self] (data, urlResponse, error) in
      	// parse data
      	...
      
      	// 2: leave group
      	self?.dispatchGroup.leave()
    }
  }
}
Posted by: Guest on May-30-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language