RACCommand使用注意
- (void)testRACCommand {
/**
* RACCommand使用注意
* 1、RACCommand内部必须返回RACSignal
* 2、executionSignals信号中的信号,一开始获取不到内部信号
* 2.1 使用switchToLatest:获取内部信号
* 2.2 使用execute:获取内部信号
* 3、executing判断是否正在执行
* 3.1 第一次不准确,需要skip:跳过
* 3.2 一定要记得sendCompleted,否则永远不会执行完成
* 4、通过执行execute,执行command的block
*/
RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {
return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@"发送第1条信号"];
[subscriber sendCompleted];
return nil;
}];
}];
/*
[command.executionSignals.switchToLatest subscribeNext:^(id _Nullable x) {
}];
*/
[[command.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
//NSLog(@"x = %@", x);
if (x.boolValue) {
NSLog(@"x = %@ 正在执行", x);
} else {
NSLog(@"x = %@ 执行完成", x);
}
}];
[command execute:@1];
}
网友评论