/Chain

Method chaining of queued closure (blocks) on GCD (Grand Central Dispatch)

Primary LanguageSwiftMIT LicenseMIT

Chain

Swift 5.0 Platforms iOS Xcode 10.2

Method chaining of queued closure (blocks) on GCD (Grand Central Dispatch).

Chain is inspired by Async.

Features

  • Method chain of queued closures (blocks) on GCD.
  • Pass the object to the next queue
  • Less code indentation.

Exsample

Basics

Chain.main {
    // called first
    // called at main thread queue
    print("start")
    return "1"
    }.background { result in
         // called second
         // called at background qos class thread queue
         print(result)  // Optional(1)
         return "2"
    }.userInteractive { result in
         // called third
         // called at userInteractive qos class thread queue
         print(result)  // Optional(2)
         return "3"
    }.userInitiated { result in
         // called fourth
         // called at userInitiated qos class thread queue
         print(result)  // Optional(3)
         return "4"
    }.onDefault { result in
         // called fifth
         // called at default qos class thread queue
         print(result)  // Optional(4)
         return "5"
    }.run(.Main) { result in
         // called last
         // called at main thread queue
         print(result)  // Optional(5)
         print("completion")
}

Custom queue

let customQueue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)

Chain.custom(customQueue) {
    // called first
    // called at customQueue
    return nil
    }.onDefault { result in
         // called second
         // called at default qos class thread queue
         return result
    }.main { result in
         // called third
         // called at main thread queue
         return result
    }.custom(customQueue) { result in
         // called fourth
         // called at customQueue
         return result
    }.run()

After

Chain.main {
    // called first
    // called at main thread queue
    print("start after: \(NSDate().description)")
    return "1"
    }.after(seconds: 5) { result in
        // called second
        // called to 5 seconds after the previous block
        // called at background qos class thread queue
        print(result)  // Optional(1)
        return "after 2: \(NSDate().description)"
    }.userInteractive { result in
        // called third
        // called at userInteractive qos class thread queue
        print(result)  // Optional(2)
        return "after 3: \(NSDate().description)"
    }.after(Queue.Utility, seconds: 5) { result in
        // called fourth
        // called to 5 seconds after the previous block
        // called at utility qos class thread queue
        print(result)  // Optional(3)
        return "after 4: \(NSDate().description)"
    }.run(.Main) { result in
        // called last
        // called at main thread queue
        print(result)  // Optional(4)
        print("after completion: \(NSDate().description)")
}

Wait

Chain.main {
    // called first
    // called at main thread queue
    print("start wait: \(NSDate().description)")
    return "1"
    }.wait(seconds: 5).background { result in
        // called to 5 seconds after the previous block
        // called at background qos class thread queue
        print("wait 2: \(NSDate().description)")
        return result
    }.wait(seconds: 5).main { result in
        // called to 5 seconds after the previous block
        // called at main thread queue
        print("wait 3: \(NSDate().description)")
        return result
    }.run()

Requirements

  • Xcode 10.2+
OS Swift
v1.2.x iOS 8+ 3.0
v1.3.x iOS 10+ 5.0

Installation

CocoaPods

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

use_frameworks!

pod "SwChain"

Carthage

To integrate Chain into your Xcode project using Carthage, specify it in your Cartfile:

github "xxxAIRINxxx/Chain"

License

MIT license. See the LICENSE file for more info.