异步操作 1.0.0

异步操作 1.0.0

Gaétan Zanella维护。



  • gaetanzanella

异步操作

Version License Platform

要求

安装

AsynchronousOperation可以通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile中

pod 'AsynchronousOperation'

用法

AsynchronousOperation是WWDC高级NSOperation会话中提供的示例代码的轻量级版本。

其主要目的是为了使创建异步任务的Operation包装更加容易,例如HTTP请求。

从子类化AsynchronousOperation或直接实例化AsynchronousBlockOperation开始。

let operationQueue = OperationQueue()

let operation = AsynchronousBlockOperation(task: { completion in
    httpManager.fetch(User.self, using: request) { result in
        // ... Handle result
        completion()
    }
})

operationQueue.addOperation(operation)

// OR

class FetchUserOperation: AsynchronousOperation {

    override func execute() {
        httpManager.fetch(User.self, using: request) { [weak self] result in
            // ... Handle result
            self?.finish()
        }
    }
}

我主要将其用于链式调用多个未同时初始化的承诺。

作者

gaetanzanella, [email protected]

许可证

异步操作可以在MIT许可证下使用。更多信息请参阅LICENSE文件。