A simple NSOperation subclass to perform asynchronous operations on NSOperationQueue. In which operation isn't finished until you invoke complete().
Mostly common for autocomplete requests when you want to perform only one async request at a time, wait for the async operation to end before exiting the queue.
Swift 3 (For Swift 2+ please use the swift_2_2 branch)
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsTo integrate OEAsyncBlockOperation into your Xcode project using CocoaPods, specify it in your Podfile:
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'OEAsyncBlockOperation'Then, run the following command:
$ pod installJust drag AsyncBlockOperation.swift file to your xcode project
self.operationQueue = OperationQueue()
self.operationQueue.maxConcurrentOperationCount = 1
...
let operation = AsyncBlockOperation.operation(withIdentifier: kBlockOperationIdentifer, queue: self.operationQueue)
weak var weakOperation = operation
operation.operationBlock = {
RequestsManager.defaultManager.performAsyncRequestWithCompletionHandler {
weakOperation?.complete()
}
}
operation.cancelBlock = {
// your cancel code here
}
self.operationQueue.addOperation(operation)
...AsyncBlockOperation.cancelAllAsyncBlockOperation(onQueue: self.operationQueue, withIdentifier: kBlockOperationIdentifer)
AsyncBlockOperation.cancelAllAsyncBlockOperation(onQueue: self.operationQueue)