swift method dispatch code example
Example 1: DispatchGroup example swift
class DispatchGroupExample: UIViewController {
private let dispatchGroup = DispatchGroup()
override func viewDidLoad() {
super.viewDidLoad()
fetchData(url: xxx)
fetchData(url: yyy)
dispatchGroup.notify(queue: .main) {
self.goToNext()
}
}
private func fetchData(url: URL) {
dispatchGroup.enter()
URLSession.shared.dataTask(with: url) {
[weak self] (data, urlResponse, error) in
...
self?.dispatchGroup.leave()
}
}
}
Example 2: swift dispatch after
let deadlineTime = DispatchTime.now() + .seconds(1)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
print("test")
}