Here comes another open source project of mine: Async.
Async is a lightweight wrapper around Grand Central Dispatch that makes it easy to perform blocks on different threads and make them dependent on each other.
Lets say, you want to execute a request (for example on the file system or the network) on a background queue and afterwards update the UI.
let fetch = {
self.birds = self.fetchLatestBirdsFromDisk()
}
let updateUI = {
self.tableView.reloadData()
}
Now, you can use Async to chain those operations together:
Async.async(QOS: .Background, block: fetch).then(QOS: .Main, block: updateUI)
An alternative way to use Async is to call methods like .Main(...) or .Background(...).
These methods do exactly the same but with a different syntax. Feel free to chose the way you like better.
Therefore, the previous example could also be written as
Async.background(block:fetch).main(after: 2, block: updateUI)
In any case, you can specify a delay (in seconds) after which the block will be executed. The delay parameter is optional and nil (=0) by default.
It was one of my goal, to avoid nesting GCD call inside GCD call and thereby reduce the number of
})
}
})
}
As usual, you can find the code on GitHub. Please let me know if you have any comments ☺️