信號的高階操作
內(nèi)容大綱
- 信號的高階操作
信號的高階操作
- 升階和降階
我們可以將一個返回 Value 的信號升階成一個返回 Signal 的信號,這種行為稱之為升階
我們也可以將一個返回 Signal 的信號降階成一個返回 Value 的信號务豺,這種行為稱之為降階
// 升階
RACSignal *arraySignal = [@[@1, @2, @3, @4, @5, @6, @8, @10, @0].rac_sequence.signal map:^RACStream *(id value) {
return [RACSignal return:value];
}];
// 降階
[[arraySignal flatten] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
- 降階操作
switchToLatests
RACSignal *signal1 = [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] mapReplace:[RACSignal return:@1]];
RACSignal *signal4 = [RACSignal return:signal1];
RACSignal *signal2 = [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] mapReplace:[RACSignal return:@"hello"]];
RACSignal *signal3 = [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] mapReplace:signal2];
[[[[signal4 merge:signal3] switchToLatest] flatten] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
signal4 是一個包含了一個定時器信號的信號,signal3 是按鈕的點擊事件觸發(fā)长踊,也包含的是一個定時器信號赛不,當(dāng)點擊按鈕時,原本定時打印1的結(jié)果改變?yōu)槎〞r打印hello。switchLatests表示纹笼,當(dāng)下一個信號來臨時,取消前一個信號的訂閱苟跪,轉(zhuǎn)而訂閱下一個信號廷痘。
If/then/else
RACSignal *ifSignal = [[self.button rac_signalForControlEvents:UIControlEventTouchUpInside] scanWithStart:@YES reduce:^id(id running, id next) {
return [running boolValue] ? @NO : @YES;
}];
RACSignal *yesSignal = [RACSignal return:@"YES"];
RACSignal *noSignal = [RACSignal return:@"NO"];
[[RACSignal if:ifSignal then:yesSignal else:noSignal] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
/*
2021-08-04 16:40:22.179744+0800 RACTestOne[38424:1864836] NO
2021-08-04 16:40:23.061356+0800 RACTestOne[38424:1864836] YES
2021-08-04 16:40:23.751064+0800 RACTestOne[38424:1864836] NO
2021-08-04 16:40:24.335030+0800 RACTestOne[38424:1864836] YES
2021-08-04 16:40:24.862587+0800 RACTestOne[38424:1864836] NO
2021-08-04 16:40:25.186937+0800 RACTestOne[38424:1864836] YES
2021-08-04 16:40:25.495577+0800 RACTestOne[38424:1864836] NO
*/
-
switch/case
根據(jù)switch訂閱對應(yīng)的signal
RACSignal *swithSignal_1 = [RACSignal return:@"1"];
RACSignal *swithSignal_2 = [RACSignal return:@"2"];
RACSignal *swithSignal_3 = [RACSignal return:@"3"];
[[RACSignal switch:swithSignal_1 cases:@{@"1": swithSignal_2} default:swithSignal_3] subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
/*
2021-08-04 16:44:57.056471+0800 RACTestOne[38484:1868134] 3
*/
-
flatten
根據(jù)時間將多個信號異步執(zhí)行
RACSignal *signal1 = [[[[[RACSignal interval:2 onScheduler:[RACScheduler mainThreadScheduler]] scanWithStart:@0 reduce:^id(id running, id next) {
return @([running integerValue] + 2);
}] take:5] map:^id(id value) {
return [RACSignal return:value];
}] delay:1];
RACSignal *signal2 = [[[[RACSignal interval:2 onScheduler:[RACScheduler scheduler]] scanWithStart:@(-1) reduce:^id(id running, id next) {
return @([running integerValue] + 2);
}] take:5] map:^id(id value) {
return [RACSignal return:value];
}];
[[[signal1 merge:signal2] flatten] subscribeNext:^(id x) {
NSLog(@"%@ -- %@", x, [NSThread currentThread]);
} completed:^{
NSLog(@"finished");
}];
/*
2021-08-04 16:52:48.891416+0800 RACTestOne[38662:1875819] 1 -- <NSThread: 0x600000321540>{number = 6, name = (null)}
2021-08-04 16:52:49.983304+0800 RACTestOne[38662:1875744] 2 -- <NSThread: 0x600000364780>{number = 1, name = main}
2021-08-04 16:52:50.891148+0800 RACTestOne[38662:1875812] 3 -- <NSThread: 0x60000032e0c0>{number = 7, name = (null)}
2021-08-04 16:52:51.991282+0800 RACTestOne[38662:1875744] 4 -- <NSThread: 0x600000364780>{number = 1, name = main}
2021-08-04 16:52:52.890137+0800 RACTestOne[38662:1875817] 5 -- <NSThread: 0x600000321000>{number = 3, name = (null)}
2021-08-04 16:52:53.923718+0800 RACTestOne[38662:1875744] 6 -- <NSThread: 0x600000364780>{number = 1, name = main}
2021-08-04 16:52:54.890292+0800 RACTestOne[38662:1875819] 7 -- <NSThread: 0x600000321540>{number = 6, name = (null)}
2021-08-04 16:52:55.966818+0800 RACTestOne[38662:1875744] 8 -- <NSThread: 0x600000364780>{number = 1, name = main}
2021-08-04 16:52:56.891232+0800 RACTestOne[38662:1875817] 9 -- <NSThread: 0x600000321000>{number = 3, name = (null)}
2021-08-04 16:52:57.951822+0800 RACTestOne[38662:1875744] 10 -- <NSThread: 0x600000364780>{number = 1, name = main}
2021-08-04 16:52:57.952246+0800 RACTestOne[38662:1875744] finished
*/
-
flattenMap
等于先map,后flatten件已,信號的返回值必須是一個信號笋额,訂閱的時候會被降階成一個值,等于先使用map篷扩,后使用flatten try