Handy NSOperationQueue
and NSBlockOperation
helpers.
SSOperations
powers various operations in my app MUDRammer - a modern MUD client for iPhone and iPad.
Install with CocoaPods. Add to your Podfile
:
pod 'SSOperations', :head # YOLO
A simple subclass of NSBlockOperation
that is passed itself as input when executed.
The primary advantage is that you can inspect, at run-time, whether the operation has been canceled and if so, clean up and exit appropriately.
SSBlockOperationBlock anOperationBlock = ^(SSBlockOperation *operation) {
if( [operation isCancelled] )
return;
// Do some stuff…
if( [operation isCancelled] )
return;
// Do some more stuff...
};
// Submit this operation to a queue for execution.
[myOperationQueue ss_addBlockOperationWithBlock:anOperationBlock];
A handy way to create an NSOperationQueue
and submit SSBlockOperationBlock
s for execution.
// An operation queue that runs operations serially.
NSOperationQueue *serialQueue = [NSOperationQueue ss_serialOperationQueue];
// An operation queue that runs up to 3 operations concurrently.
NSOperationQueue *threeOperationQueue = [NSOperationQueue ss_concurrentQueueWithConcurrentOperations:3];
// An operation queue that runs as many concurrent operations as the system deems appropriate.
// It has a name!
NSOperationQueue *concurrentQueue = [NSOperationQueue ss_concurrentMaxOperationQueueNamed:@"My queue"];
// Submit an `SSBlockOperationBlock` for processing.
[anOperationQueue ss_addBlockOperationWithBlock:anOperationBlock];
SSOperations
is a @jhersh production -- (electronic mail | @jhersh)