RAC的降阶:switchToLatest

2021-05-10  本文已影响0人  凯司机

降阶: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];
}

上一篇下一篇

猜你喜欢

热点阅读