LSBatch 用于 Objective-C 的控制流
LSBatch 通过 CocoaPods 提供访问。要安装它,只需将以下行添加到 Podfile
pod "LSBatch"
LSBatch 还可以通过 clibs 提供访问。要安装它,只需运行
$ clib install littlstar/LSBatch
或者将其添加到 package.json
文件中
...
"dependencies": {
"littlstar/LSBatch": "*"
}
...
LSBatch 提供了添加块控制流并使用一组并发调用它们的机制。LSBatch 还支持委托模式。
使用 LSBatch 与提供要执行的工作的块一样简单。该块是用以下 typedef
定义的类型
typedef void (^LSBatchWorkerCallback)(LSBatchNextCallback);
其中 LSBatchNextCallback
是应该为“工作块”的工作完成后调用的回调块。它是用以下 typedef
定义的类型
typedef void (^LSBatchNextCallback)(id <NSObject> err);
你可以提供一个在所有工作完成后执行的回调块。该块是用以下 typedef
定义的类型
typedef void (^LSBatchDoneCallback)(id <NSObject> err);
一个简单的工人示例可以这样构建
LSBatch *batch = [LSBatch new: INFINITY];
// queue work
[batch push: ^(LSBatchNextCallback next) {
// do work here
next(nil);
}];
// more work
[batch push: ^(LSBatchNextCallback next) {
// more work here
next(nil);
}];
// Execute worker blocks calling the provided
// callback block.
[batch end: ^(NSError *err) {
if (err) {
// handle error
} else {
// handle success
}
}];
LSBatch 定义了以下 LSBatchDelegate
协议
@protocol LSBatchDelegate <NSObject>
// Called when batch did finish all work.
- (void) batchDidFinish: (id <LSBatch>) batch;
// Called when batch encountered an error.
- (void) batch: (id <LSBatch>) batch didFailWithError: (id <NSObject>) err;
// Called when batch work has been aborted.
- (void) batchDidAbort: (id <LSBatch>) batch;
@end
即将推出...
MIT