便捷的NSOperationQueue
和NSBlockOperation
辅助工具。
《SSOperations》为我的应用程序中的各种操作提供动力,该应用程序是MUDRammer -适用于iPhone和iPad的现代MUD客户端。
使用CocoaPods进行安装。将其添加到您的Podfile
pod 'SSOperations', :head # YOLO
NSBlockOperation
的一个简单子类,当执行时传递自身作为输入。
主要优点是您可以在运行时检查操作是否已取消,如果是,可以适当地进行清理并退出。
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];
创建一个NSOperationQueue
并提交SSBlockOperationBlock
进行执行的一个便捷方式。
// 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];