/BetterGCD

BetterGCD 🌪 makes GCD fun! A 'swifty' wrapper for the GCD API for projects that can't yet use Swift 3

Primary LanguageSwiftMIT LicenseMIT

BetterGCD 🌪 is a highly simplistic Swift wrapper for the GCD API. While being simplistic, it allows usage of priority queues, timer-like repetition, delays, error catching, block chaining and value passing.

How can I use it?

To make an asynchronous call, just use async:

GCD().async { 
    // do something asynchronously on the main queue
}

To make an async call after 5 seconds:

GCD().after(5).async { 
    // do something asynchronously after 5 seconds
    // executed on the main queue
}

Each async call has a queue priority, which can be main, low, high. You can also set your own dispatch_queue_priority_t and flags if you want.

GCD().low().async {
    // do something asynchronously on the low priority queue
}

Async calls can be chained. Lets assume you want to do a long, low priority, task and then update something on the main queue?

GCD().low().async { 
    // do background task
}.main().async{
    // call on the main queue after first block finished
}

Each block can also be repeated, almost like a ⚡️ NSTimer:

GCD().cycle(10).after(5).async {
    // block is called 10 times
    // block is called every 5 seconds after the last block has finished
    // waits 5 seconds before being called the first time
}

Advanced chaining

BetterGCD allows you to create pipes with a generic value. This makes is less strenuous to pass on results from async calls.

GCDPipe<Int>().low().async { _ in
    // do something asynchronously on this queue
    return 42
}.main().async{ answer in
    // do something with your answer
    return nil
}

Error handling

What if an error is being thrown while execution? The pipe skips all remaining blocks and calls the catch block, if there is one. If not, the execution will end silently.

GCD().async { 
    throw Error.Fatal
}.async { 
    // never called
}.catching { error in
    print("Error: \(error) in async block")
}

Installation

BetterGCD is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'BetterGCD', '~>0.1'

License

BetterGCD is released under an MIT license. See LICENSE for more information.