NSOperation subclass for async block.
- Both compatible with Swift and Objective-C.
- Light-weight. (4KB source code. Oh my god.)
- Short-hand method extension
NSOperationQueue.
Swift
import AsyncBlockOperation
letoperation=AsyncBlockOperation{ op indoSomeAsyncTaskWithCompletionBlock{
op.complete() // complete operation
}}
queue.addOperation(operation)Objective-C
#import<AsyncBlockOperation/AsyncBlockOperation.h>
AsyncBlockOperation *operation = [AsyncBlockOperation blockOperationWithBlock:^(AsyncBlockOperation *op) {
[selfdoSomeAsyncTaskWithCompletionBlock:^{
[op complete]; // complete operation
}];
}];
[queue addOperation:operation];As NSBlockOperation does, AsyncBlockOperation supports NSOperationQueue extension to add async block operations quickly.
Swift
queue.addOperationWithAsyncBlock{ op in
op.complete()}Objective-C
[queue addOperationWithAsyncBlock:^(AsyncBlockOperation *op) {
[op complete];
}];Wanna get callback after all operations are done? Consider using NSOperationQueue+CompletionBlock which provides completionHandler for NSOperationQueue.
For example:
letqueue=NSOperationQueue()
queue.completionHandler ={println("All images are loaded!")}
queue.addOperationWithAsyncBlock{ op inloadImage(imageURL1){ image in
image.append(image)
op.complete()}}
queue.addOperationWithAsyncBlock{ op inloadImage(imageURL2){ image in
image.append(image)
op.complete()}}I recommend you to use CocoaPods, a dependency manager for Cocoa.
Podfile
pod'AsyncBlockOperation','~> 1.0'AsyncBlockOperation is under MIT license. See the LICENSE file for more info.