https://github.com/devxoul/asyncblockoperation
NSOperation subclass for async block.
https://github.com/devxoul/asyncblockoperation
Last synced: 3 months ago
JSON representation
NSOperation subclass for async block.
- Host: GitHub
- URL: https://github.com/devxoul/asyncblockoperation
- Owner: devxoul
- License: mit
- Created: 2015-07-22T12:10:41.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-07-23T19:23:20.000Z (almost 10 years ago)
- Last Synced: 2025-03-01T13:37:05.169Z (4 months ago)
- Language: Objective-C
- Homepage:
- Size: 125 KB
- Stars: 41
- Watchers: 2
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AsyncBlockOperation
===================[](https://travis-ci.org/devxoul/AsyncBlockOperation)
[](https://cocoapods.org/pods/AsyncBlockOperation)NSOperation subclass for async block.
* [x] Both compatible with Swift and Objective-C.
* [x] Light-weight. (4KB source code. Oh my god.)
* [x] Short-hand method extension `NSOperationQueue`.At a Glance
-----------**Swift**
```swift
import AsyncBlockOperationlet operation = AsyncBlockOperation { op in
doSomeAsyncTaskWithCompletionBlock {
op.complete() // complete operation
}
}
queue.addOperation(operation)
```**Objective-C**
```objc
#importAsyncBlockOperation *operation = [AsyncBlockOperation blockOperationWithBlock:^(AsyncBlockOperation *op) {
[self doSomeAsyncTaskWithCompletionBlock:^{
[op complete]; // complete operation
}];
}];
[queue addOperation:operation];
```Short-hand Method Extension
---------------------------As `NSBlockOperation` does, `AsyncBlockOperation` supports `NSOperationQueue` extension to add async block operations quickly.
**Swift**
```swift
queue.addOperationWithAsyncBlock { op in
op.complete()
}
```**Objective-C**
```objc
[queue addOperationWithAsyncBlock:^(AsyncBlockOperation *op) {
[op complete];
}];
```Further Reading
---------------Wanna get callback after all operations are done? Consider using [NSOperationQueue+CompletionBlock](https://github.com/devxoul/NSOperationQueue-CompletionBlock) which provides `completionHandler` for `NSOperationQueue`.
For example:
```swift
let queue = NSOperationQueue()
queue.completionHandler = {
println("All images are loaded!")
}
queue.addOperationWithAsyncBlock { op in
loadImage(imageURL1) { image in
image.append(image)
op.complete()
}
}
queue.addOperationWithAsyncBlock { op in
loadImage(imageURL2) { image in
image.append(image)
op.complete()
}
}
```Installation
------------I recommend you to use [CocoaPods](https://cocoapods.org), a dependency manager for Cocoa.
**Podfile**
```ruby
pod 'AsyncBlockOperation', '~> 1.0'
```License
-------**AsyncBlockOperation** is under MIT license. See the LICENSE file for more info.