监听多个信号,处理结果

场景1:界面有多个网络请求,当所有网络请求都完成时,才能展示界面。

- (void)viewDidLoad {
    [super viewDidLoad];

    RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // requestA()
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [NSThread sleepForTimeInterval:3.0];
            [subscriber sendNext:@"网络A加载完成"];
        });
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"取消A信号");
        }];
    }];
    RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // requestB()
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [NSThread sleepForTimeInterval:5.0];
            [subscriber sendNext:@"网络B加载完成"];
        });
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"取消B信号");
        }];
    }];

    [self rac_liftSelector:@selector(responseA:B:) withSignalsFromArray:@[signalA, signalB]];

}

- (void)responseA:(id)a B:(id)b {
    NSLog(@"%@, %@", a, b);
}
posted @ 2017-10-11 10:21  do+better  阅读(151)  评论(0编辑  收藏  举报