RACScheduler 信號調度器,是一個線性執行隊列,rac中的信號能夠在RACScheduler上執行任務、發送結果,底層用GCD封裝的。react
rac中提供生成線程的幾個方法:編程
1:scheduler,這是一個異步線程,不會對主線程形成堵塞,異步執行數組
[[RACScheduler scheduler] schedule:^{ NSLog(@"當前線程:%@",[RACScheduler currentScheduler]); }];
2:immediateScheduler ,當即執行的線程,其實就是在主線程執行的異步
[[RACScheduler immediateScheduler] schedule:^{ NSLog(@"當前線程:%@",[RACScheduler currentScheduler]); }];
輸出日誌: org.reactivecocoa.ReactiveObjC.RACScheduler.mainThreadSchedulerspa
3:mainThreadScheduler 獲取主線程調度器。線程
[[RACScheduler mainThreadScheduler] schedule:^{ NSLog(@"當前線程:%@",[RACScheduler currentScheduler]); }];
這個其實和immediateScheduler差很少的玩意日誌
4:currentScheduler 前幾個中就能看到,就是獲取當前線程調度器。code
[RACScheduler currentScheduler]
5:如何指定調度器的優先級。具體使用方法schedulerWithPriorityblog
rac提供了枚舉類型:繼承
RACSchedulerPriorityLow
RACSchedulerPriorityDefault
RACSchedulerPriorityBackground
RACSchedulerPriorityHigh
具體使用方法以下:
// 思考。 如何指定某個線程的優先級 [[RACScheduler schedulerWithPriority:RACSchedulerPriorityLow] schedule:^{ NSLog(@"aaaaa:%@",[RACScheduler currentScheduler]); }]; [[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault] schedule:^{ NSLog(@"bbbbb:%@",[RACScheduler currentScheduler]); }]; [[RACScheduler schedulerWithPriority:RACSchedulerPriorityBackground] schedule:^{ NSLog(@"cccccc:%@",[RACScheduler currentScheduler]); }]; [[RACScheduler schedulerWithPriority:RACSchedulerPriorityHigh] schedule:^{ NSLog(@"dddddd:%@",[RACScheduler currentScheduler]); }];
------------------ 如下列舉一下經常使用的用於調度、執行任務的方法
1: schedule 馬上執行
上面有使用案例
2:afterDelay 會將開啓的線程休眠到指定時間後執行block
// 異步線程 [[RACScheduler mainThreadScheduler] afterDelay:2 schedule:^{ NSLog(@"--------%@",[RACScheduler currentScheduler]); }];
實際使用能夠用來作延遲的操做。。。。。
3:如下是參考別的文章拿來的解釋
NSArray *temArr = @[@"1",@"2"]; [[temArr.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) { }];
大概意思就是把這個遍歷的操做放到主線程中執行了,不然遍歷的操做是在異步線程中執行的
5:subscribeOn 在設置的調度中發送信號和執行都在同一個Scheduler操做
案例
[[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { NSLog(@"sendSignal%@",[NSThread currentThread]); [subscriber sendNext:@"我是發送的數據"]; [subscriber sendCompleted]; return nil; }] subscribeOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) { NSLog(@"xxxx:%@",x); NSLog(@"receiveSignal%@",[NSThread currentThread]); }];
說明一下subscribeOn和deliverOn的區別。 其實說白了,deliverOn 會讓發送信號和接收信號不在一個線程中。就想上面的遍歷數組的例子,目的就是不想讓接受在異步中,否則得處理代碼執行順序的問題。 而subscribeOn恰好相反,會讓發送信號和接收信號在一個線程中。
// 注意數組的遍歷發送信號必定是在異步線程中執行的,,因此用subscribeOn,而後設置在主線程中接受是無效的。
代碼以下:
NSArray *temArr = @[@"1",@"2"]; [[temArr.rac_sequence.signal subscribeOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) { NSLog(@"%@",[RACScheduler currentScheduler]); }];
這個設置[RACScheduler mainThreadScheduler] 是無效的。。。。
6:timeout 超時。。。
詳細事例:
static int time = 0; [[RACSignal interval:1 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) { time ++; NSLog(@"%d",time); }]; [[[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) { return nil; }] timeout:10 onScheduler:[RACScheduler currentScheduler]] subscribeNext:^(id _Nullable x) { NSLog(@"xxxxx:%@",x); } error:^(NSError * _Nullable error) { NSLog(@"error:%@",error); }];
7:interval 這玩意就是定時器。
事例見上個案例。
---------------
ReactiveCocoa操做須知:
全部的信號(RACSignal)均可以進行操做處理,由於全部操做方法都定義在RACStream.h中,所以只要繼承RACStream就有了操做處理方法
ReactiveCocoa操做思想:
運用的是Hook(鉤子)思想,Hook是一種用於改變API(應用程序編程接口:方法)執行結果的技術.
•Hook用處:截獲API調用的技術。
Hook原理:在每次調用一個API返回結果以前,先執行你本身的方法,改變結果的輸出