美文网首页
reactivecocoa笔记

reactivecocoa笔记

作者: 磊简单 | 来源:发表于2018-11-13 17:13 被阅读0次

    RACSignal

    • 值操作 map
    例:
    RACSignal *signalB = [signalA map:^id(NSNumber *value){
          return @(value.integerValue*2);
    }];
    
    • 值操作 mapReplace
    例:
    RACSignal *signalC = [singnalA mapReplace:@8];
    
    • 值操作 reduceEach只能用在元祖上
    例子:f(x,y) = x + y
    RACSignal *signalD = [signalA reduceEach:^id(NSNumber *first,NSNumber *second){
          return @(first.integerValue + second.integerValue);
    }]
    
    • 值操作 - 其他
    -(RACSignal*)not
    -(RACSignal*)and
    -(RACSignal*)or 
    -(RACSignal*)reduceApply
    -(RACSignal*)materrialize
    -(RACSignal*)dematerialize
    
    • 数量操作
    -(RACSignal*) filter
    -(RACSignal*)ignore
    -(RACSignal*)ignoreVlues
    -(RACSignal*)distinctUntilchanged  //去重
    -(RACSignal*)take 
    例子:
    RACSignal *signalB = [signalA take:2];
    -(RACSignal*)skip
    例子:
    RACSingal *sinalC = [signalA skip:1];
    
    • 数量操作-take&skip 其他
    - (RACSignal *)takeLast:(NSUInteger)count ;
    - (RACSignal<ValueType> *)takeUntilBlock:(BOOL (^)(ValueType _Nullable x))predicate;
    - (RACSignal<ValueType> *)takeWhileBlock:(BOOL (^)(ValueType _Nullable x))predicate;
    - (RACSignal<ValueType> *)skipUntilBlock:(BOOL (^)(ValueType _Nullable x))predicate;
    - (RACSignal<ValueType> *)skipWhileBlock:(BOOL (^)(ValueType _Nullable x))predicate;
    
    • 一些混合操作
    - (RACSignal *)all:(BOOL (^)(id object))predicateBlock ;
    - (RACSignal *)any:(BOOL (^)(id object))predicateBlock ;
    - (RACSignal *)any;
    
    • 数量操作

      • startWith:

    例子:
    RACSignal *signalB = [signalA startWith:@"start"];

      * repeat:
    
    例子:
    RACSignal *signalB = [signalA repeat];
    

    ``

    • retry:
    用于网络获取。
    RACSignal *signalB = [signalA retry];
    直到成功为止
    
    • collect
    例子
    RACSignal *signalB = [signalA collect];
    
    • aggregate
    汇聚  折叠
          RACSignal * signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"1"];
            [subscriber sendNext:@"6"];
            [subscriber sendNext:@"9"];
            [subscriber sendNext:@"10"];
            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^{
    
            }];
        }];
        RACSignal *signalB = [signalA aggregateWithStart:@0 reduce:^id _Nonnull(NSNumber  * _Nonnull running, NSNumber  * _Nonnull next) {
            return @(running.integerValue + next.integerValue);
        }];
        [signalB subscribeNext:^(id  _Nullable x) {
            NSLog(@"%@",x);
        } completed:^{
    
        }];
        2018-11-13 15:54:06.977523+0800 TestReativeCocoa[22678:440319] 26
    
    • scan

            RACSignal *signalC = [signalA scanWithStart:@0 reduce:^id _Nullable(NSNumber *  _Nullable running, NSNumber *  _Nullable next) {
            return @(running.integerValue + next.integerValue);
        }];
        [signalC subscribeNext:^(id  _Nullable x) {
            NSLog(@"%@",x);
        } completed:^{
      
        }];
        2018-11-13 16:16:53.069082+0800 TestReativeCocoa[23136:450557] 1
        2018-11-13 16:16:53.069206+0800 TestReativeCocoa[23136:450557] 7
        2018-11-13 16:16:53.069326+0800 TestReativeCocoa[23136:450557] 16
        2018-11-13 16:16:53.069443+0800 TestReativeCocoa[23136:450557] 26
      
    • 时间操作 dely

        RACSignal *signalD = [signalA delay:1];
        RACSignal *interalSignal = [[[RACSignal return:@1]delay:1]repeat];
    
    • 时间操作 throttle
            RACSignal *throttleSignal = [signalA throttle:2];
            2秒时间无新信号 发出
            网络请求防重操作
          - (RACSignal *)throttle:(NSTimeInterval)interval valuesPassingTest:(BOOL (^)(id next))predicate ;
          - (RACSignal *)bufferWithTime:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler ;
    
    • 组合操作 - concat
          RACSignal *sigalE = [signalA concat:signalC];
          链接
    
    • 组合操作 - merge
          RACSignal *signalF = [signalA merge:signalB];
          signalF = [RACSignal merge:@[signalA,signalB]];
          signalF = [RACSignal merge:RACTuplePack(signalA,signalB)];
    
    • 组合操作-zip 拉链
       两个信号打包
    RACSignal *signalG = [signalA zipWith:signalA1];
        signalG = [RACSignal zip:@[signalA,signalA1]];
        signalG = [RACSignal zip:RACTuplePack(signalA,signalA1)];
        [signalA subscribeNext:^(id  _Nullable x) {
            NSLog(@"A = %@",x);
        } completed:^{
    
        }];
        [signalA1 subscribeNext:^(id  _Nullable x) {
            NSLog(@"A1 = %@",x);
        } completed:^{
    
        }];
        [signalG subscribeNext:^(id  _Nullable x) {
            NSLog(@"G = %@",x);
        } completed:^{
    
        }];
    打印:
    2018-11-13 17:07:01.322365+0800 TestReativeCocoa[24099:474095] A = 1
    2018-11-13 17:07:01.322481+0800 TestReativeCocoa[24099:474095] A = 6
    2018-11-13 17:07:01.322586+0800 TestReativeCocoa[24099:474095] A = 9
    2018-11-13 17:07:01.322695+0800 TestReativeCocoa[24099:474095] A = 10
    2018-11-13 17:07:01.322829+0800 TestReativeCocoa[24099:474095] A1 = 2
    2018-11-13 17:07:01.322935+0800 TestReativeCocoa[24099:474095] A1 = 4
    2018-11-13 17:07:01.323042+0800 TestReativeCocoa[24099:474095] A1 = 5
    2018-11-13 17:07:01.323148+0800 TestReativeCocoa[24099:474095] A1 = 8
    2018-11-13 17:07:01.323668+0800 TestReativeCocoa[24099:474095] G = <RACTuple: 0x6000024b0e20> (
        1,
        2
    )
    2018-11-13 17:07:01.323857+0800 TestReativeCocoa[24099:474095] G = <RACTuple: 0x6000024b0e40> (
        6,
        4
    )
    2018-11-13 17:07:01.325652+0800 TestReativeCocoa[24099:474095] G = <RACTuple: 0x6000024a7d70> (
        9,
        5
    )
    2018-11-13 17:07:01.325827+0800 TestReativeCocoa[24099:474095] G = <RACTuple: 0x6000024a7d90> (
        10,
        8
    )
    
    

    *组合操作 combineLatest


    98667DEE-D809-4D70-A74F-C7BA6BC17A3A.png
    • 组合操作 sample


      image.png

    如果 signalA 是景物的话 signalB就是signal的快门

    • 组合操作 takeUntil
        RACSignal *signalH = [signalA takeUntil:signalB];
    
    • 组合操作 takeUntilReplacement


      image.png

    高阶操作

    • 升阶操作
    map  产生信号
    
    • 降阶操作 - switchToLatest
      RACSignal *signalH = [signalA switchToLatest];
    
    image.png

    *降阶操作 - if/then/else with/cases/default


    image.png

    相关文章

      网友评论

          本文标题:reactivecocoa笔记

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