这是一个基于berzniz的非常优秀的Sequencer的Objective-C块序列器。
它经过扩展,允许将活动的运行与报告进度的任何UI分离。
ILActivitySequence *sequence=[ILActivitySequence activitySequence];
[sequence addActivityLabeled:@"Step 1"
block:^(ILActivity *activity, id previousResult, ILActivityProgressBlock progressBlock, ILActivityCompleteBlock completeBlock) {
NSLog(@"Running activity %@",activity.label);
// report some progress
progressBlock(activity, 1.0f);
// We are done so call the completeBlock, passing this activity's label to the next step
completeBlock(YES, activity.label, nil);
}];
[sequence addActivityLabeled:@"Step 2"
block:^(ILActivity *activity, id previousResult, ILActivityProgressBlock progressBlock, ILActivityCompleteBlock completeBlock) {
NSLog(@"Previous activity %@",previousResult);
NSLog(@"Running activity %@",activity.label);
// report some progress
progressBlock(activity, 1.0f);
// We are done so call the completeBlock, passing this activity's label to the next step
completeBlock(YES, activity.label, nil);
}];
[sequence run];
输出看起来像
Running activity Step 1
Previous activity Step 1
Running activity Step 2
在此非常基本的示例中,我们设置了一个在后台线程上运行两个步骤的序列。如果我们想当前线程上运行它并等待它运行完成,我们将最后一行更改为
[sequence runSynchronously];
一旦运行序列,它将释放自己,所以您通常不需要担心这一点。
但是,与berzniz的Sequencer不同,ActivitySequencer在运行步骤时不会释放步骤。这允许您创建可以重复运行的序列。在这种情况下,您需要自己保留和释放序列。
为了确保关注点的分离并使您的UI远离模型代码,ActivitySequencer允许您传递您可以用来通过UI报告进度或仅跟踪正在进行的块的块。
ILActivitySequence *sequence=[ILActivitySequence activitySequence];
[sequence addActivityLabeled:@"Step 1"
block:^(ILActivity *activity, id previousResult, ILActivityProgressBlock progressBlock, ILActivityCompleteBlock completeBlock) {
NSLog(@"Running activity %@",activity.label);
// report some progress
progressBlock(activity, 1.0f);
// We are done so call the completeBlock, passing this activity's label to the next step
completeBlock(YES, activity.label, nil);
}];
[sequence addActivityLabeled:@"Step 2"
block:^(ILActivity *activity, id previousResult, ILActivityProgressBlock progressBlock, ILActivityCompleteBlock completeBlock) {
NSLog(@"Previous activity %@",previousResult);
NSLog(@"Running activity %@",activity.label);
// report some progress
progressBlock(activity, 1.0f);
// We are done so call the completeBlock, passing this activity's label to the next step
completeBlock(YES, activity.label, nil);
}];
[sequence run:^(ILActivity *activity, NSInteger totalSteps) {
NSLog(@"Step %d - '%@' started.",activity.step,activity.label);
}
progress:^(ILActivity *activity, float progress) {
NSLog(@"Step %d - '%@' progress: %f.",activity.step,activity.label,progress);
}
end:^(ILActivity *activity, NSInteger totalSteps) {
NSLog(@"Step %d - '%@' ended.",activity.step,activity.label);
}
error:^(ILActivity *activity, NSError *error) {
NSLog(@"Step %d - '%@' error: %@",activity.step,activity.label,[error localizedDescription]);
}];
您将看到运行消息有4个块参数。第一个块在步骤运行之前调用。第二个块是进度块,每个活动都会调用它。第三个块在步骤运行之后调用。如果步骤没有成功运行,则调用第四个块。
输出看起来像
Step 1 - 'Step 1' started.
Running activity Step 1
Step 1 - 'Step 1' progress: 1.000000.
Step 1 - 'Step 1' ended.
Step 2 - 'Step 2' started.
Previous activity Step 1
Running activity Step 2
Step 2 - 'Step 2' progress: 1.000000.
Step 2 - 'Step 2' ended.
您可以通过将Activity Sequence文件夹从Xcode Templates文件夹复制到以下位置来安装Xcode模板:
~/Library/Developer/Xcode/Templates/File Templates
ActivitySequencer可以通过CocoaPods包管理器包括在内。
如果尚未安装,请安装CocoaPods
$ [sudo] gem install cocoapods
$ pod setup
编辑您的Podfile并添加ActivitySequencer
$ edit Podfile
pod 'ActivitySequencer'
将ActivitySequencer安装到您的Xcode项目中
$ pod install
版权(c)2013,Jon Gilkison。保留所有权利。
在满足以下条件的情况下,允许重新分发和使用源代码和二进制形式,无论是否修改:
源代码的重新分发必须保留上述版权声明、本列表中的条件以及以下免责声明。
二进制形式的重新分发必须复制上述版权声明、本列表中的条件和以下免责声明在随分发提供的文档或其他材料中。
本软件由版权所有者和贡献者提供,“现状”及其任何明确或暗示的保证,包括但不限于对适销性和针对特定用途的适用性的暗示保证均遭否认。在任何情况下,版权所有者或贡献者都不得对任何直接、间接、偶然、特殊、示范性或后果性损害(包括但不限于替代货物或服务的获取、使用、数据或利润损失或业务中断)承担责任,无论其是否已被告知此类损害的可能性,也不论责任理论是在合同、严格责任还是侵权(包括疏忽或其他)。
感谢Tal Bereznitskey提供原始想法。