iOS 开发继续加油iOS 知识点框架

IOS RAC使用 -- ReactiveObjC

2019-01-15  本文已影响4252人  Devil_Chen

前言

开始使用,要快

一、基本使用

1、基本控件

//监听文本输入
[[_textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"%@",x);
}];
//可根据自己想要监听的事件选择
[[_textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"%@",x);
    }];
//添加条件 --  下面表示输入文字长度 > 10 时才会调用subscribeNext
[[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) { 
    return value.length > 10;
}] subscribeNext:^(NSString * _Nullable x) {
     NSLog(@"输入框内容:%@", x);
}];
//监听按钮点击事件
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"-->%@",x);
    }];
//类似timer
@weakify(self)
self.disposable = [[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDate * _Nullable x) {
        @strongify(self)
        NSLog(@"时间:%@", x); // x 是当前的时间
        //关闭计时器
        [self.disposable dispose];
}];
//延时
[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@"延时2秒"];
        return nil;
    }] delay:2] subscribeNext:^(id x) {
        
        NSLog(@"-->%@",x);
    }];

2、监听属性变化

//监听self的name属性
[RACObserve(self, name) subscribeNext:^(id  _Nullable x) {
        NSLog(@"属性的改变-->%@",x);
}];
[[self rac_valuesForKeyPath:@"name" observer:self] subscribeNext:^(id  _Nullable x) {
        NSLog(@"属性的改变-->%@", x); 
}];
//此处RAC宏相当于让_label订阅了_textField的文本变化信号
//赋值给label的text属性
RAC(_label, text) = _textField.rac_textSignal;

3、遍历数组和字典

    //遍历数组
    NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
    [array.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"内容-->%@", x);
    }];
image.png

4、监听 Notification 通知事件

[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"notification" object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"-->%@", x);
}];

5、代替Delegate代理

//监听按钮点击方法的信号
//当执行完btnClickAction后会执行此订阅
[[self rac_signalForSelector:@selector(btnClickAction:)] subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"-->%@", x);
}];
-(void) btnClickAction:(UIButton *)btn
{
    NSLog(@"按钮点击");
}

二、RAC常用类

    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"🍺🍺🍺🍺🍺🍺🍺"];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"销毁了🍺🍺🍺");
        }];
    }];
    [signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    //创建RACSubject
    RACSubject *subject = [RACSubject subject];
    //订阅信号
    [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@", x);
    }];
    //发送信号
    [subject sendNext:@"信号🍺🍺🍺🍺🍺🍺🍺"];
    //通过定值创建RACTuple
    RACTuple *tuple = [RACTuple tupleWithObjects:@"1", @"2", @"3", nil];
    //利用 RAC 宏快速封装
    RACTuple *tuple2 = RACTuplePack(@"1", @"2", @"3");
    //从别的数组中获取内容
    RACTuple *tuple3 = [RACTuple tupleWithObjectsFromArray:@[@"1", @"2", @"3"]];
    
    NSLog(@"元组-->%@", tuple3[0]);
    NSLog(@"第一个元素-->%@", [tuple3 first]);
    NSLog(@"最后一个元素-->%@", [tuple3 last]);
    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"signal1-->🍺🍺🍺🍺🍺🍺🍺"];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"signal1销毁了");
        }];
    }];
    
    RACMulticastConnection *connection = [signal1 publish];
    
    [connection.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext-->1");
    }];
    [connection.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext-->2");
    }];
    [connection.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"subscribeNext-->3");
    }];
    [connection connect];
image.png
    NSString *input = @"执行";
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSLog(@"input-->%@",input);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"🍺🍺🍺🍺🍺🍺🍺"];
            [subscriber sendError:[NSError errorWithDomain:@"error" code:-1 userInfo:nil]];
//            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^{
                NSLog(@"signal销毁了");
            }];
        }];
    }];
    [command.executionSignals subscribeNext:^(RACSignal   * _Nullable x) {
        NSLog(@"executionSignals-->%@",x);
        [x subscribeNext:^(id  _Nullable x) {
            NSLog(@"executionSignals-->subscribeNext-->%@",x);
        }];
    }];
    [[command.executionSignals switchToLatest] subscribeNext:^(id  _Nullable x) {
        NSLog(@"switchToLatest-->%@",x);
    }];
    [command.executing subscribeNext:^(id  _Nullable x) {
        NSLog(@"executing-->%@",x);
    }];
    [command.errors subscribeNext:^(id  _Nullable x) {
        NSLog(@"errors-->%@",x);
    }];
    //开始执行
    [command execute:input];
image.png

三、高级函数使用

1、连接信号、合并信号等

    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"signal1-->🍺🍺🍺🍺🍺🍺🍺"];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"signal1销毁了");
        }];
    }];
    RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [subscriber sendNext:@"signal2-->🍺🍺🍺🍺🍺🍺🍺"];
        [subscriber sendCompleted];
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"signal2销毁了");
        }];
    }]; 

1.1、 concat -- 当多个信号发出的时候,有顺序的接收信号

一个信号signal3去监听signal1和signal2,必须是接收signal1完后才会接收signal2

    RACSignal *signal3 = [signal1 concat:signal2];
    [signal3 subscribeNext:^(id  _Nullable x) {
        NSLog(@"signal3-->%@",x);
    }];
image.png

1.2、 combineLatestWith -- 将多个信号合并起来,并且拿到各个信号的最新的值,必须每个合并的signal至少都有过一次sendNext,才会触发合并的信号(订阅者每次接收的参数都是所有信号的最新值),不论触发哪个信号都会触发合并的信号

一个信号signal3去监听signal1和signal2,每次回调两个信号的最新值

 RACSignal *signal3 = [signal1 combineLatestWith:signal2];
    [signal3 subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
image.png
PS:注释signal1的sendNext方法,直接销毁了,所以每个singal必须有sendNext方法 image.png

1.3、 then -- 用于连接两个信号,等待第一个信号完成,才会连接then返回的信号

RACSignal *signal3 = [signal1 then:^RACSignal * _Nonnull{
        return signal2;
    }];
    [signal3 subscribeNext:^(id  _Nullable x) {
        NSLog(@"signal3-->%@",x);
    }];
image.png

1.4、 merge -- 把多个信号合并为一个信号来监听,任何一个信号有新值的时候就会调用

一个信号signal3去监听signal1和signal2,每次回调一个信号

RACSignal *signal3 = [signal1 merge:signal2];
    [signal3 subscribeNext:^(id  _Nullable x) {
        NSLog(@"signal3-->%@",x);
    }];
image.png

1.5、 zipWith -- 把两个信号压缩成一个信号,只有当两个信号都发出信号内容时,才会触发

一个信号signal3去监听signal1和signal2,但必须两个信号都有发出(不需要同时,例如signal1信号发出了,signal2信号等了10秒之后发出,那么signal3的订阅回调是等signal2信号发出的那一刻触发)

RACSignal *signal3 = [signal1 zipWith:signal2];
    [signal3 subscribeNext:^(id  _Nullable x) {
        NSLog(@"signal3-->%@",x);
    }];
image.png

1.6、 reduce 聚合 -- 把多个信号的值按照自定义的组合返回

RACSignal *signal3 = [RACSignal combineLatest:@[signal1,signal2] reduce:^id(NSString *s1 ,NSString *s2){
        return [NSString stringWithFormat:@"%@ %@",s1,s2];
    }];
    [signal3 subscribeNext:^(id x) {
        NSLog(@"%@",x);
    }];
image.png

2、flattenMap & map 映射

//map事例
[[_textField.rac_textSignal map:^id _Nullable(NSString * _Nullable value) {
        return [NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }] ;
//flattenMap事例
 [[_textField.rac_textSignal flattenMap:^__kindof RACSignal * _Nullable(NSString * _Nullable value) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:[NSString stringWithFormat:@"%@🍺🍺🍺🍺🍺🍺🍺",value]];
            [subscriber sendCompleted];
            return [RACDisposable disposableWithBlock:^(){}];
        }];
    }] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }] ;

3、filter -- 过滤、ignore -- 忽略、distinctUntilChanged -- 忽略相同

//添加条件 --  下面表示输入文字长度 > 10 时才会调用subscribeNext
[[_textField.rac_textSignal filter:^BOOL(NSString * _Nullable value) { 
    return value.length > 10;
}] subscribeNext:^(NSString * _Nullable x) {
     NSLog(@"输入框内容:%@", x);
}];
//忽略掉值为"123"的信号
[[_textField.rac_textSignal ignore:@"123"] subscribeNext:^(id x) {
    NSLog(@"%@",x);
}];
    RACSubject *subject = [RACSubject subject];
    [[subject distinctUntilChanged] subscribeNext:^(id  _Nullable x) {
        NSLog(@"-->%@",x);
    }];
    [subject sendNext:@"123"];
    [subject sendNext:@"123"];
    [subject sendNext:@"123"];
    [subject sendNext:@"🍺🍺🍺🍺🍺🍺"];
    [subject sendNext:@"🍺🍺🍺🍺🍺🍺"];
    [subject sendCompleted];
image.png
上一篇下一篇

猜你喜欢

热点阅读