Answers for "swift method dispatch"

0

swift dispatch after

let deadlineTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
    print("test")
}
Posted by: Guest on December-21-2020
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