美文网首页
RAC的降阶:switchToLatest

RAC的降阶:switchToLatest

作者: 凯司机 | 来源:发表于2021-05-10 18:45 被阅读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];
    }
    
    

    相关文章

      网友评论

          本文标题:RAC的降阶:switchToLatest

          本文链接:https://www.haomeiwen.com/subject/qfvirltx.html