Floc Commands 是 Objective-C 中的一系列易于使用命令的便捷集合。命令通常包含一小部分同步或异步逻辑,可以根据需要自由地链式或嵌套。Floc Commands 包含一系列实用的命令,例如:
所有命令都是 FLCommand 的子类,它被设计为可以同步或异步使用。
要开始使用,创建一个 FLCommand 的子类,并重写 execute 方法。当命令执行完成时,它必须始终调用 didExecuteWithError:。如果没有错误成功执行,您可以将参数传递为 nil,或为了方便起见使用 didExecute。分配一个委托以响应该 commandWillExecute:,command:didExecuteWithError: 或 commandCancelled:。
Floc Commands 包含一些有用的类别和宏,以启用一些酷炫的功能,例如:
FLSQ(dowloadImage, convertImage, applyFilter, upload).stopsOnError(YES).retry(3)
        .intercept(showSuccessAlert, showErrorAlert)
        .slave(playJeopardyTheme.repeat(-1)).keepAlive.execute;在此示例中,FLSQ(宏为 FLSequenceCommand)依次执行所有异步命令,并且在任何错误发生时将会重试执行所有命令。如果序列失败,将执行 showErrorAlert,否则执行 showSuccessAlert。playJeopardyTheme 将无限重复(-1),并与序列一起执行,一旦异步操作完成即取消,因为我们没有对命令的强指针,所以调用 keepAlive 确保命令不会在完成之前被回收。为您管理命令的生命周期。
FLBC(^(FLBlockCommand *command) {
    NSLog(@"na");
    [command performSelector:@selector(didExecute) withObject:nil afterDelay:0.2];
}).repeat(16).flseq(FLBC(^(FLBlockCommand *command) {
    NSLog(@"Batman!");
    [command performSelector:@selector(didExecute) withObject:nil afterDelay:0.2];
})).repeat(-1).keepAlive.execute;此示例创建了一个重复 16 次的 FLBlockCommand,然后是一个另一个 FLBlockCommand。该序列将无限重复(-1)
为每个命令类型都有方便的宏。查看它们!请参阅FLCommand+Floc.h
在处理异步命令时,应该保持对其的强引用,否则它在完成前可能会被释放。或者,您可以调用keepAlive,这样命令的生命周期将由您来管理。
RequestDataCommand可能需要几秒钟才能完成,因此将其分配给属性以保持命令的活跃状态是个好主意。
self.requestCommand = [[[RequesteDataCommand alloc] init] execute];
// or
[[RequesteDataCommand alloc] init].keepAlive.execute;@implementation HelloWorldCommand
- (void)execute {
    [super execute];
    NSLog(@"Hello world!");
    [self didExecute];
}
@end@implementation SendDataCommand
- (void)execute {
    [super execute];
      // This may take a few seconds...
      [self.service sendData:data onComplete:^(NSError *error) {
          [self didExecuteWithError:error];
      }];
}
- (void)cancel {
    [self.service closeConnection];
    [super cancel];
}
@end逐个执行命令。可用选项
stopOnError将在发生错误时停止cancelOnCancel将在子命令被取消时取消[[FLSequenceCommand alloc] initWithCommands:@[c1, c2, c3, c4]];同时执行所有命令。可用选项
stopOnError将在发生错误时停止cancelOnCancel将在子命令被取消时取消[[FLPrallelCommand alloc] initWithCommands:@[c1, c2, c3, c4]];执行目标命令。当无错误发生时,执行成功命令,否则执行错误命令。可用选项
cancelOnCancel将在子命令被取消时取消forwardTargetError将通过command:didExecuteWithError:转发目标错误self.postCommand = [[FLInterceptionCommand alloc] initWithTarget:sendDataCommand
                                                         success:hideSpinnerCommand
                                                           error:showErrorAlertCommand];
[self.postCommand execute];[[FLBlockCommand alloc] initWithBlock:^(FLBlockCommand *command) {
    NSLog(@"Hello world!");
    [command didExecute];
}];FLBlockCommand *delayCommand = [[FLBlockCommand alloc] initWithBlock:^(FLBlockCommand *command) {
    [command performSelector:@selector(didExecute) withObject:nil afterDelay:1];
}];同时执行两个命令,并且在主命令执行后立即取消奴隶。可用选项
forwardMasterError将通过command:didExecuteWithError:转发主错误[[FLMasterSlaveCommand alloc] initWithMaster:doHeavyTaskCommand slave:playJeopardyMusicCommand];...以及更多(《FLRepeatCommand》、《FLRetryCommand》、《FLDelayCommand》)
您在Floc-Commands/Classes中找到您需要的源文件。
$ cd path/to/project
$ pod install
打开创建的Xcode Workspace文件。