降阶:switchToLatest
RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@1];
[subscriber sendNext:@2];
[subscriber sendNext:@3];
[subscriber sendNext:@4];
[subscriber sendNext:@5];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"signalA完成");
}];
}];
RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
return [RACDisposable disposableWithBlock:^{
NSLog(@"signalB完成");
}];
}];
RACSignal *signalC = [AppDelegate if:[RACSignal return:@(YES)] then:signalA else:signalB];
[[signalC subscribeNext:^(id x) {
NSLog(@"subscribeNext:%@",x);
}] dispose];
// if/then/else本质&类似操作
+ (RACSignal *)if:(RACSignal *)boolSignal then:(RACSignal *)trueSignal else:(RACSignal *)falseSignal {
// 降阶操作
return [[boolSignal map:^id(NSNumber *value) {
return (value.boolValue ? trueSignal : falseSignal);
}] switchToLatest];
}
网友评论