Dispatch is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Dispatch'
Dispatch.async(dispatch_get_main_queue()) {
//Code to be run on the main thread
}
Dispatch.async(Queue.main) {
//Code to be run on the main thread
}
Dispatch.async {
//Code to be run on the main thread
}
Dispatch.async(Queue.main) {
//Code to be run on the main thread
}
let someCustomQueue = dispatch_queue_create("custom.queue.dispatch", DISPATCH_QUEUE_CONCURRENT)
Dispatch.sync(someCustomQueue) {
//Code to be synchronously on someCustomQueue
}
Dispatch.after(1.0, queue: Queue.main) {
//Code to be run on the main thread after 1 second
}
Dispatch.after(1.0) {
//Code to be run on the main thread after 1 second
}
let token : dispatch_once_t
Dispatch.once(&token) {
//Code to be run only once in App lifetime
}
let mainQueue = Queue.main
let customConcurrentQueue = Queue.custom("custom.concurrent.queue.dispatch", Queue.Atribute.concurrent)
let customSerialQueue = Queue.custom("custom.serial.queue.dispatch", Queue.Atribute.serial)
let priority = 0 // or you use one of the Global priorities (ex: Queue.Priority.UserInteractive)
let globalQueue = Queue.global(priority)
// For comodity there are helpers for getting the Global queues
let globalUserInteractiveQueue = Queue.globalUserInteractive
let globalUserInitiatedQueue = Queue.globalUserInitiated
let globalUtilityQueue = Queue.globalUtility
let globalBackgroundQueue = Queue.globalBackground
- Chainable methods
- Unit Tests
- Travis CI
- More examples
-
João Mourato, joao.armourato@gmail.com
-
Gabriel Peart
Dispatch is available under the MIT license. See the LICENSE file for more info.