测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布日期最后发布 | 2017年9月 |
SwiftSwift 版本 | 4.0 |
SPM支持 SPM | ✓ |
由 Luca Iaconelli 维护。
SCHRAsync 的目的是提供一种简单的方式来布局要异步执行的宏步骤流,然后执行它们。也许在某一步中,您想使用库 X 从某些 URL 下载所有图片,而在另一步中,您想使用库 Y 来排队进行图像处理。
Swift 包管理器 是一种自动化 Swift 代码分发的工具,并集成到 Swift 编译器中。它处于早期开发阶段,但 SCHRAsync 支持其在支持平台上的使用。
一旦您设置了 Swift 包,将 SCHRAsync 添加为依赖项就像将其添加到 Package.swift 中的依赖项值一样简单。
dependencies: [
.Package(url: "https://github.com/Luca9307/SCHRAsync.git")
]
只需创建步骤并使用 SCHRAsync 来运行它们
let stepOne = SCHRStep { stepper, previousResult in
// previousResult in the first step is nil
// code to be executed on Step 1
stepper.next(result: stepOneResult)
}
let stepTwo = SCHRStep { stepper, previousResult in
// previousResult in the second step is the result of step 1
// The error can be anything as long as it conforms to the ErrorType protocol
// code to be executed on Step 2
stepper.abort(error: someError)
}
SCHRAsync(steps: stepOne,stepTwo).start()
在步骤闭包中,不要忘了使用一些结果或错误来完成步骤。否则,SCHRAsync 将在此处停止。
SCHRAsync 提供了您可以在流程开始、完成、由于错误结束或取消时运行的闭包。所有处理闭包都将在主线程上运行。
SCHRAsync(steps: stepOne,stepTwo).always {
// code to be run when flow start.
}.start()
SCHRAsync(steps: stepOne,stepTwo).finish { finishState in
// code to be run after all steps finish
}.start()
如果您以错误完成步骤,流程将结束。
SCHRAsync(steps: stepOne,stepTwo).error { error in
// code to be executed if a step failed. The error will be sent here
}.onFinish { finishState in
// If one step is finished with error. This block will not be executed.
// Unless, no error closure is provided, in which case the SCHRAsync will
// default to this closure with finishState = .Failed(let error)
}.start()
如果流程被取消。
let a = SCHRAsync(steps: stepOne,stepTwo).cancel{
// code to be executed if the flow is canceled.
}.finish{ finishState in
// If the flow is canceled. This block will not be executed.
// Unless, no cancel closure is provided, in which case the SCHRAsync will
// default to this closure with finishState = .Canceled
}
a.start()
a.cancel()
您还可以使用 SCHRAsync 以这种方式
SCHRAsync().always {
//Initial code
}.then { stepper, result in
// previousResult in the first step is nil
// code to be executed on Step 1
stepper.next(result)
}.then { stepper, result in
// previousResult in the second step is the result of step 1
// The error can be anything as long as it conforms to the ErrorType protocol
// code to be executed on Step 2
stepper.next(result)
}.finish { state in
// code to be run after all steps finish
}.start()
SCHRAsync 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。