AsyncOperation is a custom class for create Asynchronous Operation for OperationQueue
- iOS 11.0+
- Swift 5.0+
You can use The Swift Package Manager to install AsyncOperation by adding https://github.com/quangnghiadev/AsyncOperation.git
to Swift Package of your XCode project
Create your sub class of AsyncOperation with async task on main
function. For example, WorkerOperation
below receive a closure for run async task.
typealias WorkerCallback = () -> Void
final class WorkerOperation: AsyncOperation {
var task: ((@escaping WorkerCallback) -> Void)?
init(task: ((@escaping WorkerCallback) -> Void)?) {
self.task = task
}
override func main() {
task? { [weak self] in
self?.finish()
}
}
}
Create an instance and add it into operation queue
private var worker: WorkerOperation = WorkerOperation(task: { callback in
DispatchQueue.global().asyncAfter(deadline: .now() + 1) {
callback()
}
})
private let operationQueue = OperationQueue()
operationQueue.addOperation(worker)
That's all!
- AsyncOperation is released under the MIT license. See LICENSE for more information.