Reputation: 1589
I have a few processes that need to be completed in order when my TableView loads. I would expect that it would wait until the code completed before executing the next line of code, but it seems that's not the case. Is there a way to have these wait until completion before executing the next step?
override func viewDidLoad() {
super.viewDidLoad()
performTask1()
performTask2()
performTask3()
}
Thanks for all the help!
Upvotes: 4
Views: 3011
Reputation: 438417
The typical example to make each of these methods take a completionHandler
parameter, e.g.:
func perform1(completionHandler: () -> Void) {
doSomethingAsynchronously() {
completionHandler()
}
}
func perform2(completionHandler: () -> Void) {
doSomethingElseAsynchronously() {
completionHandler()
}
}
func perform3(completionHandler: () -> Void) {
doSomethingCompletelyDifferentAsynchronously() {
completionHandler()
}
}
Then you can run them like so:
override func viewDidLoad() {
super.viewDidLoad()
perform1 {
self.perform2 {
self.perform3 {
}
}
}
}
Upvotes: 4