AsyncOperation
Operation 的泛型子类,用于异步获取结果。
用法
// Define success type
struct MyOperationSuccess {
// …
}
// Make subclass of AsyncOperation whose success type is `MyOperationSuccess`
class MyOperation: AsyncOperation<MyOperationSuccess> {
// …
override func didStartExecuting() {
// Do something…
// When completed, call `self.complete()` with `MyOperationSuccess` instance
// When failed, call `self.fail()` with `Error`
}
}
// Make `MyOperation` instance
let op = MyOperation()
// Start the operation with `completionBlock`. Alternatively, you can add the operation into `OperationQueue`.
op.start {
do {
let result = try op.getResult() // Type of 'result' is `MyOperationSuccess`
// Do something with result…
} catch AsyncOperationError.cancelled(let canceller) {
// Handle cancellation…
} catch {
// Handle other errors…
}
}
更多
提供 AsyncBlockOperation
以便使用块创建操作。
需求
- Swift 5.0 或更高版本
- iOS 11.0.1 或更高版本
- Mac OS T.B.D.
- watchOS T.B.D.
- tvOS T.B.D.
安装
Carthage
- 在 Cartfile 中插入
github "zuccoi/AsyncOperation"
。 - 运行
carthage update
。 - 在
Carthage/Build
中使用AsyncOperation.framework
将应用链接起来。
CocoaPods
- 在 Podfile 中插入
pod 'ZUAsyncOperation'
。 - 运行
pod install
。
手动
将 Source 文件夹下的文件复制到您的项目中(例如 AsyncOperation.swift)。
许可协议
MIT。