RACLearing (ReactiveCocoa 2.5)

#import "ViewController.h"
#import "RedView.h"
#import "NSObject+RACKVOWrapper.h"
#import "Flag.h"
#import "NextViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountLabel;
@property (weak, nonatomic) IBOutlet UITextField *pwdLabel;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@property (nonatomic) id<RACSubscriber> subscriber;
@property (weak, nonatomic) IBOutlet UITextField *txtField;
@property (weak, nonatomic) IBOutlet UILabel *txtLabel;
@property (weak, nonatomic) IBOutlet RedView *redView;
@property (weak, nonatomic) IBOutlet UIButton *btn;

@property (nonatomic) RACSignal *signal;

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
//    NextViewController *next = [[NextViewController alloc] init];
//    [self presentViewController:next animated:YES completion:nil];
    
//    [self RACSequence];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[@[@"you", @"are", @"beautiful"] .rac_sequence.signal map:^id(id value) {
        return [value capitalizedString];
    }] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
}

//RAC常用的宏
- (void)RACMacros {
    
    //1.RAC
//    [_txtField.rac_textSignal subscribeNext:^(id x) {
//        _txtLabel.text = x;
//    }];
    //用宏更简单,用来给某个对象的属性绑定一个信号,只要产生信号内容,就会把内容给属性赋值
    RAC(_txtLabel, text) = _txtField.rac_textSignal;
    
    //2.监听某个对象的某个属性
    [RACObserve(self.view, frame) subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //3.@weakify, @strongify 防止循环引用
    @weakify(self);
    _signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self);
        NSLog(@"%@", self.signal);
        return nil;
    }];
    [_signal subscribeNext:^(id x) {
        
    }];
    
    
    //4.包装元组
    RACTuple *tuple = RACTuplePack(@1, @2);
    NSLog(@"%@", tuple[0]);
    
    
}

//当一个界面有多次请求的时候,需要保证所有的请求完成才能搭建界面
- (void)lifrSelector {
    //请求第一个模块
    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        //请求数据AFN
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [subscriber sendNext:@"第一个模块"];
        });
        
        return nil;
    }];
    //请求第二个模块
    RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        //请求数据AFN
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [subscriber sendNext:@"第二个模块"];
        });
        
        return nil;
    }];
    
    //数组存放信号
    //当数组中的所有信号都发送数据的时候才会执行方法
    //方法是有要求的,方法的参数必须与数组的信号一一对应
    [self rac_liftSelector:@selector(updateUI: and:) withSignalsFromArray:@[signal1, signal2]];
}
- (void)updateUI:(NSString *)str1 and:(NSString *)str2 {
    
    NSLog(@"两个模块加载完成--%@--%@", str1, str2);
}

- (void)RACReplace {
    //1.代替代理
    //需要传值得时候用RACSubject,不需要的时候用下面这种方法
    [[_redView rac_signalForSelector:@selector(btnClick2:)] subscribeNext:^(id x) {
        NSLog(@"红色按钮被点击了");
    }];
    
    //2.代替kvo
    [_redView rac_observeKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
        
    }];
    
    [[_redView rac_valuesForKeyPath:@"frame" observer:nil] subscribeNext:^(id x) {
       //x就是修改的值;
        NSLog(@"%@", x);
    }];
    
    
    //3.监听事件
    [[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        NSLog(@"按钮被点击了");
    }];
    
    //4.代替通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    
    //5.监听文本框
    [_txtField.rac_textSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    _redView.frame = CGRectMake(0, 0, 200, 200);
}

//NSArray NSDictionary遍历
- (void)RACSequence {
    
    NSArray *array = @[@"111", @"222", @2];
    
    //NSArray遍历
    //数组转集合
    RACSequence *sequence = array.rac_sequence;
    //把集合转化为信号
    RACSignal *signal = sequence.signal;
    //遍历数组
    [signal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //同上
    [array.rac_sequence.signal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    /*
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
    NSMutableArray *mutArray = [NSMutableArray array];
    [array.rac_sequence.signal subscribeNext:^(NSDictionary *dict) {
        Flag *flag = [Flag flagWithDict:dict];
        [mutArray addObject:flag];
    }];
    
    //高级用法,代替上面
    //把集合中所有的元素映射成为一个新的对象
    NSArray *array1 = [[array.rac_sequence map:^id(NSDictionary *value) {
        
        //value 集合中的元素
        //id:返回对象就是映射的值
        return [Flag flagWithDict:value];
    }] array];
    NSLog(@"%@", array1);
    */
    
    
    
    //NSDictionary遍历
    NSDictionary *dict = @{@"account": @"name", @"name": @"xmg", @"age": @18};
    [dict.rac_sequence.signal subscribeNext:^(RACTuple *tuple) {
//        NSString *key = tuple[0];
//        NSString *value = tuple[1];
//        
//        NSLog(@"%@: %@", key, value);
        
        //或者用RAC的宏
        //参数需要穿解析出来的变量名
        RACTupleUnpack(NSString *key, NSString *value) = tuple;
        
        NSLog(@"%@: %@", key, value);
        
    } completed:^{
        NSLog(@"字典遍历完成");
    }];
    
}

//RAC集合类
- (void)RACTuple {
    RACTuple *tuple = [RACTuple tupleWithObjectsFromArray:@[@"111", @"222", @3]];
    NSString *str = tuple[0];
    NSNumber *num = tuple[2];
    
    NSLog(@"%@", num);
}

- (void)redViewTest {
    [_redView.btnClickSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
}

- (void)RACReplaySubject {
    //创建信号 RACReplaySubject 是 RACSubject子类 但是可以先发送,再订阅
    RACReplaySubject *subject = [RACReplaySubject subject];
    
    //发送信号 先保存值,然后遍历所有的订阅者去发送数据
    [subject sendNext:@"sss"];
    
    //订阅信号 遍历所有的值,然后拿到当前订阅者去发送数据
    [subject subscribeNext:^(id x) {
        NSLog(@"11---%@", x);
    }];
    [subject subscribeNext:^(id x) {
        NSLog(@"22---%@", x);
    }];
}

//RACSubject用法
- (void)RACSubject {
    //RACSubject 信号提供者,也可以发送信号(一个发送,多个接收)
    //1.创建信号
    RACSubject *subject = [RACSubject subject];
    
    //先订阅信号,不同信号订阅的方式不一样,subject仅仅是保存订阅者到数组中
    [subject subscribeNext:^(id x) {
        NSLog(@"subject1 -- %@", x);
    }];
    [subject subscribeNext:^(id x) {
        NSLog(@"subject2 -- %@", x);
    }];
    
    //再发送信号subject从数组遍历取出所有订阅者发送数据
    [subject sendNext:@"subject"];
}

//取消订阅信号
- (void)RACDisposable {
    //1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        //只要订阅者在,就不会自动取消订阅
        _subscriber = subscriber;
        //3.发送信号
        [subscriber sendNext:@"sss"];
        
        //默认一个信号发送数据完毕以后就会被主动取消订阅
        return [RACDisposable disposableWithBlock:^{
            //信号取消订阅就会来到这个block
            NSLog(@"信号被取消订阅");
        }];
    }];
    
    //2.订阅信号
    RACDisposable *disposable = [signal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //主动取消订阅
    [disposable dispose];
}

//创建信号
- (void)RACSignal {
    //    RAC使用步骤
    //1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        //didSubscribe调用,只要信号被订阅就会调用
        //didSubscribe作用:发送数据
        
        //3.发送信号
        [subscriber sendNext:@1];
        
        return nil;
    }];
    //2.订阅信号
    
    [signal subscribeNext:^(id x) {
        //nextBlock调用:只要订阅者发送数据就会调用
        //nextBlock作用:处理数据,展示到UI上
        //x 就是信号发送的内容
        
        NSLog(@"%@", x);
        
    }];
    
    //订阅者只要调用sendNext,就会执行nextBlock
    //只要信号被订阅,就会执行didSubscribe
    //前提条件是RACDynamicSignal,不同类型的订阅,处理的事情不一样
}

@end
#import "NextViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "NSObject+RACKVOWrapper.h"
#import "BindViewController.h"


@interface NextViewController ()

@end

@implementation NextViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    BindViewController *bind = [[BindViewController alloc] init];
    [self presentViewController:bind animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    
    
//    [self RACCommand];
    

    
}

//switchToLatest:获取信号中的信号发送的最新的信号
- (void)signalOfSignals {
    //高级用法
    //创建信号中的信号
    RACSubject *signalOfSignals = [RACSubject subject];
    RACSubject *subject = [RACSubject subject];
    RACSubject *subject1 = [RACSubject subject];
    //订阅信号
    [signalOfSignals subscribeNext:^(RACSignal *x) {
        [x subscribeNext:^(id x) {
            NSLog(@"%@", x);
        }];
    }];
    //switchToLatest:获取信号中的信号发送的最新的信号
    [signalOfSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //发送信号
    [signalOfSignals sendNext:subject];
    [subject sendNext:@"ss"];
}

//RAC中用于处理事件的类,可以把事件如何处理,事件中的数据如何传递,包装到这个类中,他可以方便的监听事件的执行过程
- (void)RACCommand {
    //1.创建命令,
    //不能返回一个空的信号
    
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        //input 就是execute传入的参数
        //block调用的时刻:只要执行命令的时候就会调用
        
        NSLog(@"%@", input);
        //不能返回nil
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:@"执行命令产生的数据"];
            
            //当命令发送完成,一定要主动发送执行完成
            [subscriber sendCompleted];
            
            return nil;
        }];
        
    }];
    
    //监听事件有没有完成
    [command.executing subscribeNext:^(id x) {
        if ([x boolValue]) {
            NSLog(@"%@ 正在执行", x);
        } else {
            NSLog(@"%@ 执行完成/没有执行", x);
        }
        
    }];
    
    
    //下面的这些方法必须放在[command execute:@2]; 前面,不然不会执行
    //如何拿到执行命令中产生的数据
    //订阅命令内部的信号
    
    /*
    //1.方式一:直接订阅执行命令返回的信号
    RACSignal *signal = [command execute:@"2"];
    [signal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    */
    
    /*
    //2.方式二
    //不许要在执行命令前被订阅
    //订阅信号
    //executionSignals:信号源,信号中的信号。发送数据几句是信号
    [command.executionSignals subscribeNext:^(id x) {
        [x subscribeNext:^(id x) {
            NSLog(@"%@", x);
        }];
        NSLog(@"%@", x);
    }];
    
    
    //2.执行命令
    [command execute:@2];
    */
    
    //switchToLatest:获取最新发送的信号,只能用于信号中的信号
    [command.executionSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    [command execute:@2];
    
    
}

// RACMulticastConnection 用于当一个信号被多次调用的时候,为了保证创建信号时,避免多次调用创建信号中的block造成副作用,可以使用这个类处理
- (void)RACMulticastConnection {
    //每次订阅都不要都请求一次,指向请求一次,每次订阅只拿到数据
    //不管订阅多少次信号,就会请求一次
    
    //1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSLog(@"发送热门模块的请求");
        
        [subscriber sendNext:@22];
        
        return nil;
    }];
    //2.把信号转化成链接类
    RACMulticastConnection *connection = [signal publish];
//    [signal multicast:signal]; //这个方法也可以
    
    //3.订阅链接类的信号 //NSLog(@"发送热门模块的请求") 只会发送一次,而@22会发送多次
    [connection.signal subscribeNext:^(id x) {
        NSLog(@"1.%@", x);
    }];
    
    [connection.signal subscribeNext:^(id x) {
        NSLog(@"2.%@", x);
    }];
    
    [connection.signal subscribeNext:^(id x) {
        NSLog(@"3.%@", x);
    }];
    
    //4.链接
    [connection connect];
    
}

- (void)RACSignal {
    //    RACMulticastConnection 用于当一个信号被多次调用的时候,为了保证创建信号时,避免多次调用创建信号中的block造成副作用,可以使用这个类处理
    //例如,下面这个,每次请求都会打印“发送热门模块的请求”,而有时只想要@“11”这个值
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送热门模块的请求");
        [subscriber sendNext:@"11"];
        return nil;
    }];
    
    [signal subscribeNext:^(id x) {
        NSLog(@"1.%@", x);
    }];
    
    [signal subscribeNext:^(id x) {
        NSLog(@"2.%@", x);
    }];
    
    [signal subscribeNext:^(id x) {
        NSLog(@"3.%@", x);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#import "BindViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
#import "NSObject+RACKVOWrapper.h"
#import "CombineViewController.h"
#import <ReactiveCocoa/RACReturnSignal.h>

@interface BindViewController ()

@end

@implementation BindViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    CombineViewController *com = [[CombineViewController alloc] init];
    [self presentViewController:com animated:YES completion:nil];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
//    [self signalOfSignals];
}

- (void)signalOfSignals {
    RACSubject *signalOfSignals = [RACSubject subject];
    RACSubject *subject = [RACSubject subject];
    
    //订阅信号
//    [signalOfSignals subscribeNext:^(id x) {
//        [x subscribeNext:^(id x) {
//            NSLog(@"%@", x);
//        }];
//    }];
    /*
    RACSignal *bindSignal = [signalOfSignals flattenMap:^RACStream *(id value) {
        return value;
    }];
    [bindSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
     */
    
    //上面的写法太麻烦,开发中用下面的写法
    [[signalOfSignals flattenMap:^RACStream *(id value) {
        return value;
    }] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //发送信号
    [signalOfSignals sendNext:subject];
    [subject sendNext:@"12"];
}

//
- (void)RACMap {
    //创建信号
    RACSubject *subject = [RACSubject subject];
    
    //绑定 返回任意想要的类型
    RACSignal *bindSignal = [subject map:^id(id value) {
        return @"111";
    }];
    [bindSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    [subject sendNext:@"aa"];
}

//一般用于信号中的信号
- (void)RACFlattenMap {
    //创建信号
    RACSubject *subject = [RACSubject subject];
    
    //绑定 返回的RACStream *类型
    RACSignal *bindSignal = [subject flattenMap:^RACStream *(id value) {
        //value:原信号发送的内容
        //返回的信号,就是要包装的值
        return [RACReturnSignal return: [NSString stringWithFormat:@"包装的%@", value]];
    }];
    //订阅信号
    [bindSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //发送数据
    [subject sendNext:@"ss"];
}

//绑定
- (void)RACBind {
    //使用bind对原信号进行处理,然后返回
    //1.创建信号
    RACSubject *subject = [RACSubject subject];
    
    //2.hock绑定信号
    RACSignal *bindSignal = [subject bind:^RACStreamBindBlock{
        return ^RACSignal *(id value, BOOL *stop) {
            //block调用:只要原信号发送数据,就会调用block
            //value:原信号发送的内容
            NSLog(@"%@", value);
            //返回信号,不能传nil,如果想要传nil,用下面方法代替
            value = [NSString stringWithFormat:@"处理以后的信号+%@", value];
            return [RACReturnSignal return:value];
        };
    }];
    
    //3.订阅绑定信号
    [bindSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    //4.发送数据
    [subject sendNext:@"222"];
}
@end
#import "CombineViewController.h"
#import <ReactiveCocoa/ReactiveCocoa.h>

@interface CombineViewController ()

@end

@implementation CombineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self skip];
}

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

//如果当前的值跟上一次相同,就不会被订阅到
- (void)distinctUntilChanged {
    RACSubject *subject = [RACSubject subject];
    
    [[subject distinctUntilChanged] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    [subject sendNext:@1];
    [subject sendNext:@1];
}

//获取几次信号
- (void)take {
    RACSubject *subject = [RACSubject subject];
    RACSubject *subject1 =[RACSubject subject];
    //取前面几个值
    [[subject take:1] subscribeNext:^(id x) {
        NSLog(@"take:%@", x);
    }];
    
    //取后面几个值(必须发送 sendCompleted)
    [[subject takeLast:1] subscribeNext:^(id x) {
        NSLog(@"takeLast:%@", x);
    }];
    
    //只要传入的信号发送完成,或者发送任意数据(发送错误不行),就不会接收到原信号的内容
    [[subject takeUntil:subject1] subscribeNext:^(id x) {
        NSLog(@"takeUntil:%@", x);
    }];
    
    [subject sendNext:@"1"];//会发送
    [subject1 sendCompleted];
    [subject sendNext:@"13"];//不会发送
    [subject sendCompleted];
}

//忽略
- (void)ignore {
    RACSubject *subject = [RACSubject subject];
    
    //忽略信号
    RACSignal *ignoreSignal = [subject ignore:@"1"];
    
    //订阅
    [ignoreSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    [subject sendNext:@"1"];//会被忽略
    [subject sendNext:@"13"];
}

//过滤
- (void)filter {
    UITextField *txt1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    [self.view addSubview:txt1];
    
    [[txt1.rac_textSignal filter:^BOOL(id value) {
        return [value length] > 5;
    }] subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
}

//combine来组合信号
- (void)combineAndReduce {
    UITextField *txt1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
    [self.view addSubview:txt1];
    UITextField *txt2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 100, 40)];
    [self.view addSubview:txt2];
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn.frame = CGRectMake(100, 210, 100, 40);
    btn.backgroundColor = [UIColor orangeColor];
    btn.enabled = NO;
    [self.view addSubview:btn];
    
    
    //需求:只有两个输入框都有输入的时候按钮才能点击
    //组合 combine:组合, reduce:聚合
    //reduceBlock参数:跟组合信号有关,一一对应
    RACSignal *combineSignal = [RACSignal combineLatest:@[txt1.rac_textSignal, txt2.rac_textSignal] reduce:^id(NSString *account, NSString *pwd){
        
        NSLog(@"account:%@ -- pwd:%@", account, pwd);
        //聚的值就是组合信号的内容
        //只要原信号发送内容就会调用,组合成一个系的值
        
        return @(account.length && pwd.length);
    }];

    //订阅
//    [signal subscribeNext:^(id x) {
//        btn.enabled = [x boolValue];
//    }];
    
    //下面方法代替上面的方法
    RAC(btn, enabled) = combineSignal;
    
}

//zipWith来组合信号 :一个界面多个请求的时候,等多个秦秋完成了才能更新UI,这时候用zipWith来组合信号
- (void)zipWith {
    //信号A
    RACSubject *signalA = [RACSubject subject];
    //信号B
    RACSubject *signalB = [RACSubject subject];
    
    //信号C
    RACSignal *signalC = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了C的请求");
        
        [subscriber sendNext:@"C"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    //组合信号 结果与发送顺序无关,与组合顺序有关
    RACSignal *zipSignal = [[signalA zipWith:signalB] zipWith:signalC];
    [zipSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    
    [signalB sendNext:@"B"];
    [signalA sendNext:@"A"];
}

//merge 来组合信号,任意一个信号请求完成,都会被订阅到,无顺序
- (void)merge {
    //信号A
    RACSubject *signalA = [RACSubject subject];
    //信号B
    RACSubject *signalB = [RACSubject subject];
    
    //信号C
    RACSignal *signalC = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了C的请求");
        
        [subscriber sendNext:@"C的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    //组合信号 merge
    RACSignal *mergeSignal = [[signalA merge:signalB] merge:signalC];
    
    [mergeSignal subscribeNext:^(id x) {
        //任意一个信号发送的内容都会来到这里
        NSLog(@"%@", x);
    }];
    
    
    [signalB sendNext:@"B部分"];
    [signalA sendNext:@"A部分"];
    
}

//then来组合信号
- (void)then {
    //信号A
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了A的请求");
        
        [subscriber sendNext:@"A的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    //信号B
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了B的请求");
        
        [subscriber sendNext:@"B的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    //信号C
    RACSignal *signalC = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了C的请求");
        
        [subscriber sendNext:@"C的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    //组合信号 then 会忽略上面信号的值,拿到下面部分的值
    RACSignal *thenSignal = [[signalA then:^RACSignal *{
        //返回的信号就是要组合的信号
        return signalB;
    }] then:^RACSignal *{
        return signalC;
    }];
    //订阅
    [thenSignal subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];

}

//RAC组合信号
- (void)concat {
    
    //信号A
    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了A的请求");
        
        [subscriber sendNext:@"A的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    //信号B
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了B的请求");
        
        [subscriber sendNext:@"B的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    //信号C
    RACSignal *signalC = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSLog(@"发送了C的请求");
        
        [subscriber sendNext:@"C的数据"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    //按照顺序去请求
    //创建组合信号
    RACSignal *concatSignal = [[signalA concat:signalB] concat:signalC];
    //订阅组合信号
    [concatSignal subscribeNext:^(id x) {
        //既能拿到A的信号值,又能拿到B信号的值,掐提示,每次都要发送completed
        NSLog(@"%@", x);
    }];
}

@end

demo地址https://github.com/belink-QD/RACLearning

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容