码迷,mamicode.com
首页 > 其他好文 > 详细

OC+RAC(六) 核心方法bind

时间:2019-05-03 18:53:39      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:csu   div   span   ext   pos   ber   class   str   code   

-(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

标签:csu   div   span   ext   pos   ber   class   str   code   

原文地址:https://www.cnblogs.com/liuw-flexi/p/10805828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!