SwChain 1.3.0

SwChain 1.3.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新版本2019年3月
SPM支持 SPM

airin 维护。



SwChain 1.3.0

  • Airin

链式

Swift 5.0 Platforms iOS Xcode 10.2

在 GCD (Grand Central Dispatch) 上对队列中关闭块(块)进行链式处理。

Chain 受到 Async 的启发。

特性

  • 在 GCD 上对队列中关闭块(块)进行链式处理。
  • 将对象传递到下一个队列
  • 更少的代码缩进。

示例

基本

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")
}

自定义队列

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()

之后

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)")
}

等待

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()

需求

  • Xcode 10.2+
操作系统 Swift
v1.2.x iOS 8+ 3.0
v1.3.x iOS 10+ 5.0

安装

CocoaPods

Chain 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile 文件中

use_frameworks!

pod "SwChain"

Carthage

要使用 Carthage 将 Chain 集成到 Xcode 项目中,请在 Cartfile 文件中指定它

github "xxxAIRINxxx/Chain"

许可协议

MIT 许可协议。更多信息请参阅 LICENSE 文件。