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

RACCommand中的信号

时间:2015-08-09 23:58:57      阅读:1407      评论:0      收藏:0      [点我收藏+]

标签:

示例:

 RACSignal* textSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@(1)];
        [subscriber sendNext:@(2)];
        [subscriber sendError:[NSError new]];
        return nil;
    }];
    
    RACCommand* textCommad = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return textSignal;
    }];
    
    self.createButton.rac_command = textCommad;
    
    [textCommad.executing subscribeNext:^(id x) {
        NSLog(@"executing%@",x);
    }];
    
    [textCommad.executionSignals subscribeNext:^(id x) {
        NSLog(@"executionSignals%@",x);
    }];
    
    [[textCommad.executionSignals switchToLatest]subscribeNext:^(id x) {
        NSLog(@"executionSignals switchLatest%@",x);
    }];
    
    [textCommad.errors subscribeNext:^(id x) {
        NSLog(@"errors");
    }];

输出:

2015-08-09 22:17:27.610 ReactiveCocoaDemo[7181:141024] executing0

2015-08-09 22:17:30.325 ReactiveCocoaDemo[7181:141024] executing1

2015-08-09 22:17:30.325 ReactiveCocoaDemo[7181:141024] executionSignals<RACDynamicSignal: 0x7f89b3c6ca20> name: 

2015-08-09 22:17:30.326 ReactiveCocoaDemo[7181:141024] executionSignals switchLatest1

2015-08-09 22:17:30.326 ReactiveCocoaDemo[7181:141024] executionSignals switchLatest2

2015-08-09 22:17:30.327 ReactiveCocoaDemo[7181:141024] errors

2015-08-09 22:17:30.327 ReactiveCocoaDemo[7181:141024] executing0

结论:

1.executing信号一绑定就会sendNext:@(NO);如果想忽略第一次的Next,使用[executing skip:1]

2.按钮点击事件发生时,首先exectuing会sendNext:@(YES);然后executionSignals会sendNext一个RACSignal对象,该对象就是RACCommand创建时传入的block的返回值。

3.注意executionSignals是信号的信号,即它的值类型为RACSignal,而我们一般希望捕获的是RACSignal所携带的值,因此可以使用switchToLatest或flatten的方法来做到。

4.errors包含了RACComand执行过程产生的所有错误。

5.等到RACCommand中的Signal都完毕了(complete或error),exectuting会sendNext:@(NO).

RACCommand中的信号

标签:

原文地址:http://www.cnblogs.com/guoxiaoqian/p/4716540.html

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