iOS RAC常用方法总结

简介:本文主要对RAC常见使用方法及易混淆的概念和注意事项进行汇总,方便后续查询和学习。原文请参考:《ReactiveCocoa 概述》《zwcshy/RAC》常见问题请参考《iOS RAC常见问题汇总》

1.代替代理

//代理
[[self rac_signalForSelector:@selector(tableView:didSelectRowAtIndexPath:) fromProtocol:@protocol(UITableViewDelegate)] subscribeNext:^(id x) {
            
}];

2.监听事件

按钮点击

[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
}];

文本内容变化

[[self.textField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
     
}];

手势事件

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
[tap.rac_gestureSignal subscribeNext:^(id x) {

}];

3.通知

[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"name" object:nil] subscribeNext:^(id x) {
        
}];

4.KVO

//方法1
[[self rac_valuesAndChangesForKeyPath:@"title" options:NSKeyValueObservingOptionNew observer:nil] subscribeNext:^(id x) {
        
}];
//方法2
[[self rac_valuesForKeyPath:@"title" observer:nil] subscribeNext:^(id x) {
        
}];
//方法3
[RACObserve(self, title) subscribeNext:^(id x) {
        
}];

5.定时器

//定时器
RACDisposable * disposable = [[RACSignal interval:1 onScheduler:[RACScheduler scheduler]] subscribeNext:^(id x) {
        
}];
    
//释放定时器
[disposable dispose];

6.集合遍历RACSequence

数组遍历

//默认在子线程中遍历
NSArray *numbers = @[@"1",@"2",@"3",@"4"];
[numbers.rac_sequence.signal subscribeNext:^(id x) {
  NSLog(@"%@",x);
}];
//放在主线程中遍历
[[numbers.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id  _Nullable x) {
        
}];

字典遍历

NSDictionary * dic = @{
    @"1":@"1111",
    @"2":@"2222",
    @"3":@"3333",
    @"4":@"4444"
};

[dic.rac_sequence.signal subscribeNext:^(id x) {
    RACTupleUnpack(NSString * key,NSObject * value) = x;
    NSLog(@"key=%@,value=%@",key,value);
}];

字符串遍历

NSString *text = @"123456789";
[text.rac_sequence.signal subscribeNext:^(id x) {
   NSLog(@"%@",x);
}];

7.映射Map(生成新的值)flattenMap(生成新的信号)

map(生成新的值)

NSArray * newNumbers = [numbers.rac_sequence map:^id(id value) {
    return  [NSString stringWithFormat:@"numbers: %@",value];
}].array;

flattenMap(生成新的信号)

// 创建信号
RACSubject *subject = [RACSubject subject];
// 绑定信号
RACSignal *bindSignal = [subject flattenMap:^RACStream *(id value) {
// value: 就是源信号发送的内容
// 返回信号用来包装成修改内容的值
return [RACReturnSignal return:value];

}];
// flattenMap中返回的是什么信号,订阅的就是什么信号(那么,x的值等于value的值,如果我们操纵value的值那么x也会随之而变)
// 订阅信号
[bindSignal subscribeNext:^(id x) {
NSLog(@"%@", x);
}];

// 发送数据
[subject sendNext:@"123"];

8.延时执行

throttle:延时调用block(subscribeNext)

[[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] throttle:5] subscribeNext:^(id x) {
        
}];

delay:延迟调用

[[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] delay:5] subscribeNext:^(id x) {
         
}];

timeout:超时后则不调用block

[[self.signal timeout:5 onScheduler:[RACScheduler scheduler]] subscribeNext:^(id x) {
}];

延时执行

[[RACScheduler scheduler] after:[NSDate dateWithTimeIntervalSinceNow:3] schedule:^{
           
 }];

9.过滤

filter接受满足条件的信号

[[numbers.rac_sequence.signal filter:^BOOL(id value) {
    return [value integerValue] > 3;
}] subscribeNext:^(id x) {
        
}];
// 只有当文本框的内容长度大于5,才获取文本框里的内容
[[self.textField.rac_textSignal filter:^BOOL(id value) {
// value 源信号的内容
return [value length] > 5;
// 返回值 就是过滤条件。只有满足这个条件才能获取到内容
}] subscribeNext:^(id x) {
  NSLog(@"%@", x);
}];

skip跳过几个信号

// skip:后边传入要跳过几个信号
RACSubject *subject = [RACSubject subject];
[[subject skip:2] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
[subject sendNext:@1];
[subject sendNext:@2];
[subject sendNext:@3];

distinctUntilChanged新值与旧值不一样则接收

//例子1
RACSubject *subject = [RACSubject subject];
[[subject distinctUntilChanged] subscribeNext:^(id x) {
  NSLog(@"%@", x);
}];
// 发送信号
[subject sendNext:@1];
[subject sendNext:@2];
[subject sendNext:@2]; // 不会被订阅
//例子2
[[RACObserve(self, title) distinctUntilChanged] subscribeNext:^(id x) {
        
}];

take只拿前几个值

RACSubject *subject = [RACSubject subject];
[[subject take:2] subscribeNext:^(id x) {
  NSLog(@"%@", x);
}];
// 发送信号
[subject sendNext:@1];
[subject sendNext:@2];
[subject sendNext:@3];

takeLast取最后几个值,takeLast 一定要调用sendCompleted,告诉他发送完成才能取到最后的几个值

RACSubject *subject = [RACSubject subject];
[[subject takeLast:2] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
// 发送信号
[subject sendNext:@1];
[subject sendNext:@2];
[subject sendNext:@3];
[subject sendCompleted];

takeUntil给takeUntil传的是哪个信号,那么当这个信号发送信号或sendCompleted,就不能再接受源信号的内容了

RACSubject *subject = [RACSubject subject];
RACSubject *subject2 = [RACSubject subject];
[[subject takeUntil:subject2] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
// 发送信号
[subject sendNext:@1];
[subject sendNext:@2];
[subject2 sendNext:@3];  // 1
//    [subject2 sendCompleted]; // 或2
[subject sendNext:@4];

ignore:忽略一些值
ignoreValues:表示忽略所有的值

// 1.创建信号
RACSubject *subject = [RACSubject subject];
// 2.忽略一些值
RACSignal *ignoreSignal = [subject ignore:@2]; // ignoreValues:表示忽略所有的值
// 3.订阅信号
[ignoreSignal subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
// 4.发送数据
[subject sendNext:@2];

10.RAC组合

combine场景:账号和密码都有值,登录按钮才可点击

//reduce里的参数一定要和combineLatest数组里的一一对应。
RACSignal *combinSignal = [RACSignal combineLatest:@[self.accountField.rac_textSignal, self.pwdField.rac_textSignal] reduce:^id(NSString *account, NSString *pwd){ NSLog(@"%@ %@", account, pwd);
  return @(account.length && pwd.length);
}];

RAC(self.loginBtn, enabled) = combinSignal;

merge多个信号合并成一个信号,任何一个信号有新值就会调用

// 创建信号A
RACSubject *signalA = [RACSubject subject];
// 创建信号B
RACSubject *signalB = [RACSubject subject];
//组合信号
RACSignal *mergeSignal = [signalA merge:signalB];
// 订阅信号
[mergeSignal subscribeNext:^(id x) {
  NSLog(@"%@", x);
}];
// 发送信号---交换位置则数据结果顺序也会交换
[signalB sendNext:@"下部分"];
[signalA sendNext:@"上部分"];

concat:串行执行,第一个信号必须要调用sendCompleted

// 创建信号A
RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id subscriber) {
  [subscriber sendNext:@"上部分数据"];
  [subscriber sendCompleted]; // 必须要调用sendCompleted方法!
  return nil;
}];

// 创建信号B,
RACSignal *signalsB = [RACSignal createSignal:^RACDisposable *(id subscriber) {
  [subscriber sendNext:@"下部分数据"];
  return nil;
}];

// 创建组合信号
RACSignal *concatSignal = [signalA concat:signalsB];
// 订阅组合信号
[concatSignal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];

其他方法

doNext:添加额外执行代码

[[[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x){
    //开始loading
}] subscribeNext:^(id x){
    //结束loading
}];

你可能感兴趣的:(iOS RAC常用方法总结)