iOS开发RAC学习笔记(八)信号的调度器RACScheduler(线程)

github源代码地址
信号的调度器RACScheduler(多线程)
deliverOn 在上一篇我们讲到过RAC的副作用,deliverOn这个方法会将内容传递切换到指定线程,而副作用依旧会在指定线程内执行

//创建信号 -(void)createUpSignals{RACSignal * signal = [RACSignal createSignal:^RACDisposable *(id subscriber) {NSLog(@"sendTestSignal%@",[NSThread currentThread]); [subscriber sendNext:@1]; [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{}]; }]; self.testSignal = signal; }//订阅信号 //要想放在主线程执行只要将[RACScheduler scheduler]更换为[RACScheduler mainThreadScheduler] [[self.testSignal deliverOn:[RACScheduler scheduler]] subscribeNext:^(id x) {NSLog(@"receiveSignal%@",[NSThread currentThread]); }];

  • 打印结果如下
    2016-09-02 09:48:59.697 Signal processing[1686:22894] sendTestSignal{number = 1, name = main} 2016-09-02 09:48:59.697 Signal processing[1686:24680] receiveSignal{number = 3, name = (null)}

subscribeOn则会将传递内容和副作用一起放到指定线程执行
[[[RACSignal createSignal:^RACDisposable *(id subscriber) {NSLog(@"sendSignal%@",[NSThread currentThread]); [subscriber sendNext:@1]; return [RACDisposable disposableWithBlock:^{}]; }] subscribeOn:[RACScheduler scheduler]] subscribeNext:^(id x) {NSLog(@"receiveSignal%@",[NSThread currentThread]); }];

  • 【iOS开发RAC学习笔记(八)信号的调度器RACScheduler(线程)】打印结果如下
    2016-09-02 09:54:47.819 Signal processing[1778:54504] sendSignal{number = 2, name = (null)} 2016-09-02 09:54:47.819 Signal processing[1778:54504] receiveSignal{number = 2, name = (null)}

    推荐阅读