Giter Club home page Giter Club logo

bettergcd's Introduction

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.

bettergcd's People

Contributors

sebastian-hojas avatar

Stargazers

七 avatar Caglar Cakar avatar Ross Barbish avatar Denis Zhabbarov avatar Oliver Stenbom avatar Harimoto avatar Yusuf Kıldan avatar Ilter Cengiz avatar Ondra Kandera avatar Matej Ľach avatar Russell Cook avatar Christopher Walmsley avatar Ngo Quoc Thang avatar Zakk Hoyt avatar  avatar

Watchers

Irrelevant avatar Matej Ľach avatar James Cloos avatar

Forkers

matejlach jadenis

bettergcd's Issues

Consider adding UIViewAnimation

Would be nice to have a convenience wrapper for

UIView.animateWithDuration(0.3, animations: {
    self.skipButton.alpha = 0.0
    self.continueButton.alpha = 1.0
})

to

UIView.animate(0.3) {
    self.skipButton.alpha = 0.0
}.animate(0.3) {
    // also allows chaining of animations
}

Might make things a bit more complicated because of the many options the animation allows. It's also has nothing to do with GCD.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.