OC+RAC 核心方法bind

Posted liuw-flexi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OC+RAC 核心方法bind相关的知识,希望对你有一定的参考价值。

-(void)_test6{
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@"first value"];
        [subscriber sendNext:@"second value"];
        [subscriber sendNext:@"third value"];
        [subscriber sendCompleted];
        return nil;
    }];
    
    RACSignal *bindSignal = [signal bind:^RACSignalBindBlock{
        
        return  ^RACSignal *(id value, BOOL *stop) {
            
            NSString *oValue = value;
            if ([oValue isEqualToString:@"first value"]) {
                return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                    [subscriber sendNext:@"first value bind after"];
                    [subscriber sendCompleted];
                    return nil;
                }];
            }
            
            if ([oValue isEqualToString:@"second value"]) {
                *stop = YES;
                return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                    [subscriber sendNext:@"second value bind after"];
                    [subscriber sendCompleted];
                    return nil;
                }];
            }
            
            if ([oValue isEqualToString:@"third value"]) {
                return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                    [subscriber sendNext:@"third value bind after"];
                    [subscriber sendCompleted];
                    return nil;
                }];
            }
            
            return nil;
        };
    }];
    
    [bindSignal subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"x====%@",x);
    }];
    
}
再来一个例子:


-(void)_test7{
    //1.创建信号
    RACSubject * subject = [RACSubject subject];
    
    //2.绑定信号
    RACSignal * bindSignal = [subject bind:^RACSignalBindBlock _Nonnull{
        
        return ^RACSignal * (id value, BOOL *stop){
            NSLog(@"%@",value);
            return [RACReturnSignal return:[NSString stringWithFormat:@"一顿操作猛如虎 %@",value]];
            
        };
    }];
    
    //3.订阅信号
    [bindSignal subscribeNext:^(id  _Nullable x) {
        NSLog(@"绑定接收到!! %@",x);
    }];
    
    //4.发送
    [subject sendNext:@"发送原始的数据"];
}

 

以上是关于OC+RAC 核心方法bind的主要内容,如果未能解决你的问题,请参考以下文章

RAC - RACSignal

IOS开发-OC学习-常用功能代码片段整理

浅谈ReactiveCocoa之MVVM

RAC-iOS中基本用法

OC + RAC RACSignal 基本使用

OC + RAC RACMulticastConnection