美文网首页
ReactiveCocoa操作

ReactiveCocoa操作

作者: kwalter | 来源:发表于2016-08-19 11:22 被阅读70次

    map :映射

    textField.rac_textSigal map:^id(NSString* value){

    return @(value.length);

    } subscribeNext:^(NSString* x){

    NSLog(@“%@“,x);

    }

    打印:

    0  1  2  3  4

    filter : 过滤

    textField.rac_textSigal filter:BOOL(NSString* value){

    return value.length > 3;

    }

    delay: 延时

    设置延时两秒后发送消息

    [[[RACSignal createSignal:^RACDisposable*(id subscriber){

    [subscriber sendNext:@“ricke”];

    [subscriber sendCompleted];

    return nil;

    }] delay:2]

    subscribeNext:^(id x){

    NSLog(@“%@”,x);

    }];

    startWith: 在发送消息之前,先发送一个消息

    [[[RACSignal createSignal:^RACDisposable* (id subscriber){

    [subscriber sendNext:@“next”];

    [subscriber sendCompleted];

    return nil;

    }] startWith:@“RAC”]

    subscriberNext:^(id x){

    NSLog(@“%@”,x);

    }];

    timeout :超时

    [[[RACSignalcreateSignal:^RACDisposable* (idsubscriber){

    [[RACSchedulermainThreadScheduler]afterDelay:3schedule:^{

    [subscribersendNext:@"sned"];

    [subscribersendCompleted];

    }];

    returnnil;

    }]timeout:2onScheduler:[RACSchedulermainThreadScheduler]]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    take :发送多个消息的时候,取最前面的几条

    [[[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    [subscribersendNext:@"1"];

    [subscribersendNext:@"2"];

    [subscribersendNext:@"3"];

    [subscribersendNext:@"4"];

    [subscribersendNext:@"5"];

    [subscribersendCompleted];

    returnnil;

    }]take:2]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    takelast : 发送多条消息的时候,取最后面的几条

    [[[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    [subscribersendNext:@"1"];

    [subscribersendNext:@"2"];

    [subscribersendNext:@"3"];

    [subscribersendNext:@"4"];

    [subscribersendNext:@"5"];

    [subscribersendNext:@"6"];

    [subscribersendCompleted];

    returnnil;

    }]takeLast:3]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    takeUntil :RACSignal (发送在takeUntil后面的信号完成前的消息)

    //RAC这个消息是2秒后完成,所以1、2这两个消息是可以发送到,而3秒后3、4就不会发送

    [[[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    [subscribersendNext:@"1"];

    [subscribersendNext:@"2"];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

    [subscribersendNext:@"3"];

    [subscribersendNext:@"4"];

    [subscribersendCompleted];

    });

    [subscribersendCompleted];

    returnnil;

    }]takeUntil:[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

    [subscribersendNext:@"RAC"];

    [subscribersendCompleted];

    });

    returnnil;

    }]]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    takeWhileBlock :但takeWhileBlock返回YES的时候发送消息

    [[[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    [subscribersendNext:@"1"];

    [subscribersendNext:@"2"];

    [subscribersendNext:@"3"];

    [subscribersendNext:@"4"];

    [subscribersendNext:@"5"];

    [subscribersendCompleted];

    returnnil;

    }]takeWhileBlock:^BOOL(idx) {

    returnYES;

    }]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    skip: 跳过

    [[[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    [subscribersendNext:@"1"];

    [subscribersendNext:@"2"];

    [subscribersendNext:@"3"];

    [subscribersendNext:@"4"];

    [subscribersendNext:@"5"];

    [subscribersendNext:@"6"];

    [subscribersendCompleted];

    returnnil;

    }]skip:2]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    skipWhileBlock 和skipUntilBlock

    skipWhileBlock :当block返回YES的时候跳过,返回NO的时候发送

    skipUntilBlock:skipUntilBlock和skipWhileBlock相反,当block返回NO的时候跳,返回YES的时候发送

    throttle (节流)优化请求时间间隔

    throttle: 设置一个间隔时间,当两次信号之间的时间差小于这个时间就不会发送请求

    [[textField.rac_textSignalthrottle:0.3]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    distinctUntilChanged: 优化请求字符一样的时候(当后一个请求和钱一个请求一样的时候,就可以不用请求)

    [[[textField.rac_textSignalthrottle:0.3]distinctUntilChanged]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    ignore : 忽略某些 例如上面的空字符

    [[[[textField.rac_textSignalthrottle:0.3]distinctUntilChanged]ignore:@""]

    subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    switchToLatest : 当你请求数据的时候、网络会有延迟,数据还没有返回回来,这时候你发送了新的请求,则我们取消前一次的请求,只发送最新的请求

    [[[[[[textField.rac_textSignalthrottle:0.3]distinctUntilChanged]ignore:@""]

    map:^id(idvalue) {

    return[RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    [subscribersendNext:@"request"];

    [subscribersendCompleted];

    returnnil;

    }];

    }]switchToLatest]

    subscribeNext:^(idx) {

    NSLog(@"--request---%@",x);

    }];

    多个信号处理

    merge :同时订阅2个信号

    RACSignal* signalA = [RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

    [subscribersendNext:@"SignalA"];

    [subscribersendCompleted];

    });

    returnnil;

    }];

    RACSignal* signalB = [RACSignalcreateSignal:^RACDisposable*(id subscriber) {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

    [subscribersendNext:@"signalB"];

    [subscribersendCompleted];

    });

    returnnil;

    }];

    [[signalAmerge:signalB]subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    concat

    singleA 完成后才会订阅signalB有点像串行队列 A失败了B就不会被订阅

    [[signalAconcat:signalB]subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    zipWith 和  combineLatest: 每个信号都至少要发送一次才可以被订阅

    [[signalAzipWith:signalB]subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    [[RACSignalcombineLatest:@[signalA,signalB]]subscribeNext:^(idx) {

    NSLog(@"%@",x);

    }];

    RAC中的宏:

    RAC(self.titleLabel,text) = [RACSignal interval:1onScheduler:[RACScheduler mainThreadScheduler]]map:^id(NSData* value) {

    returnvalue.description;

    }

    属性监听

    RACObserve(self.menuButton, enabled)

    相关文章

      网友评论

          本文标题:ReactiveCocoa操作

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