ReactiveCocoa进阶(2)

2017-09-16  本文已影响7人  攻克乃还_
所有的RACSignal都可以进行操作处理,因为所有的操作方法都定义在Stream.h中,因此只要继承了RACStream就有了操作处理的方法

ReactiveCocoa操作思想:

- Hook(钩子)思想:改变API(应用程序编程接口,也就是方法)执行结果

一、ReactiveCocoa核心方法:bind

// 需求:每次在文本框的输入后面+sun
[[_textField.rac_textSignal bind:^RACSignalBindBlock _Nonnull{
    return ^RACSignal *(id value, BOOL *stop){
        // 信号一改变,就会执行,并且把值传递过来
        NSString *result = [NSString stringWithFormat:@"%@%@",value,@"sun"];
        return [RACReturnSignal return:result];
    };
}] subscribeNext:^(id  _Nullable x) {
    NSLog(@"获取到处理完的数据 %@",x);
}];

二、ReactiveCocoa操作方法之映射(flattenMap,map)

[[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
    NSString *result = [NSString stringWithFormat:@"xmg %@",value];
    return result;
}] subscribeNext:^(id  _Nullable x) {
    NSLog(@"%@",x);
}];
[[_textField.rac_textSignal flattenMap:^ RACSignal * (NSString * value) {
    NSString *result = [NSString stringWithFormat:@"sun %@",value];
    return [RACReturnSignal return:result];
}] subscribeNext:^(id  _Nullable x) {
    NSLog(@"%@",x);
}];
[[signalOfSignals flattenMap:^RACSignal *(id value) {
    return [value map:^id _Nullable(id  _Nullable value) {
        return [NSString stringWithFormat:@"XMG:%@",value];
    }];
}] subscribeNext:^(id  _Nullable x) {
    NSLog(@"1-%@",x);
}];

三、ReactiveCocoa操作方法之组合

3.1.concat

concat语法:
    // 创建信号
    RACSubject *signalA = [RACSubject subject];
    RACSubject *signalB = [RACReplaySubject subject];
    NSMutableArray *arrM = [NSMutableArray array];
    // concat
    [[signalA concat: signalB] subscribeNext:^(id  _Nullable x) {
        [arrM addObject:x];
    }];
    // 发送信号
    [signalB sendNext:@"B"];
    [signalA sendNext:@"A"];
    [signalA sendCompleted];

3.2.then

then语法:
RACSignal *signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
    [subscriber sendNext:@1];
    [subscriber sendCompleted];
    return nil;
}];
[[signalA then:^RACSignal * _Nonnull{
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@2];
        return nil;
    }];
}] subscribeNext:^(id  _Nullable x) {
    NSLog(@"%@",x);
}];

3.3.merge

    // 无序的整合信号数据
    RACSubject *signalA = [RACSubject subject];
    RACSubject *signalB = [RACSubject subject];
    [[signalA merge:signalB] subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    // 发送
    [signalB sendNext:@"B"];
    [signalA sendNext:@"A"];

3.4.zipWith

RACSubject *signalA = [RACSubject subject];
RACSubject *signalB = [RACSubject subject];
[[signalA zipWith:signalB] subscribeNext:^(id  _Nullable x) {
    RACTupleUnpack(NSString *a,NSString *b) = x;
    NSLog(@"%@ %@",a,b);
 }];   
[signalA sendNext:@"A"];
[signalB sendNext:@"B"];

3.5.combineLatest (组合)

[[_accountField.rac_textSignal combineLatestWith:_pwdField.rac_textSignal] subscribeNext:^(id  _Nullable x) {
    RACTupleUnpack(NSString *account,NSString *pwd) = x;
    _loginButton.enabled = account.length > 0 && pwd.length;
}];

3.6.reduce(聚合)

// 常见用法:先组合再聚合
[[RACSignal combineLatest:@[_accountField.rac_textSignal,_pwdField.rac_textSignal] 
   reduce:^id (NSString *account, NSString *pwd){
   //这里返回什么,下面Block的x就是什么
   return @(account.length > 0 && pwd.length > 0);
}] subscribeNext:^(id  _Nullable x) {
    _loginButton.enabled = [x boolValue];
}];

四、ReactiveCocoa核心操作方法-filter(过滤)

[[_pwdField.rac_textSignal filter:^BOOL (NSString *value) {
    // 必须要满足这个条件,才可以给订阅者发送数据
    return value.length > 6;
}] subscribeNext:^(NSString * _Nullable x) {
    NSLog(@"%@",x);
}];

五、ReactiveCocoa核心操作方法-定时器

interval: onScheduler:

[[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] 
subscribeNext:^(NSDate * _Nullable x) {
    NSLog(@"执行了定时器");
}];

六、ReactiveCocoa核心操作方法-delay(延迟执行)

// 延迟发送数据
[[[RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
    [subscriber sendNext:@"hello"];
    return nil;
}] delay:2] subscribeNext:^(id  _Nullable x) {
    NSLog(@"%@",x);
}];

遵循NSFastEnumeration协议的可以当做数组

上一篇下一篇

猜你喜欢

热点阅读