Skip to content

AsyncOperation is a custom class for create Asynchronous Operation for OperationQueue

License

Notifications You must be signed in to change notification settings

quangnghiadev/AsyncOperation

Repository files navigation

Build Status SPM compatible Swift Xcode MIT

AsyncOperation is a custom class for create Asynchronous Operation for OperationQueue

Requirements

  • iOS 11.0+
  • Swift 5.0+

Installation

Swift Package Manager

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

Usage

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!

License

  • AsyncOperation is released under the MIT license. See LICENSE for more information.

About

AsyncOperation is a custom class for create Asynchronous Operation for OperationQueue

Topics

Resources

License

Stars

Watchers

Forks

Languages