1.使用concat来按顺序合并多个信号为一个信号
- 按照合并的顺序来接受数据,先发送的数据先接受到
- 但是先发送的必须调用sendCompleted, 否则后发送的数据会接收不到
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号1数据"];
[subscriber sendCompleted];
return nil;
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号2数据"];
[subscriber sendCompleted];
return nil;
}];
// //写法1
// RACSignal *concatSignal = [signal1 concat: signal2];
//写法2
RACSignal *concatSignal = [RACSignal concat: @[signal1, signal2]];
[concatSignal subscribeNext:^(id _Nullable x) {
NSLog(@"合并信号 订阅: %@",x);
}];
2.使用merge来合并多个信号为一个信号
区分concat!!!!
- merge会按照merge顺序来接受信号,但是非阻塞的。
- 如果前边顺序的信号没有发送数据,而后面的信号发送了数据,那么依然后接受后边的信号
- 无需添加
sendCompleted
也依然会接受信号。 - 简单理解为,参与合并的多个信号只要有数据来,合并信号就会接受。
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
return nil;
}];
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号1发送"];
return nil;
}];
RACSignal *signal3 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号3发送"];
return nil;
}];
// 写法1 RACSignal *mergeSignal = [signal1 merge: signal2];
RACSignal *mergeSignal = [RACSignal merge: @[signal3, signal2, signal1]];
[mergeSignal subscribeNext:^(id _Nullable x) {
NSLog(@"mergeSignal : %@", x);
}];
3.使用combineLatest来按照顺序合并多个信号的最新值为元组
- 会取参与合并的信号的最新值合并
- 合并结果是按照合并顺序生成的元组
-
combineLatest
可以有参数,但是必须跟合并信号的个数一一对应
RACSubject *signal1 = [RACSubject subject];
RACSubject *signal2 = [RACSubject subject];
RACSignal *combineSignal = [RACSignal combineLatest: @[signal2, signal1]];
[combineSignal subscribeNext:^(id _Nullable x) {
NSLog(@"combine : %@",x);
}];
[signal2 sendNext: @"2"];
[signal1 sendNext: @"1"];
[signal1 sendNext: @"1111111"];
[signal2 sendNext: @"2222222"];
[signal2 sendNext: @"hahahah"];
进阶用法: 使用combineLatest参数
RACSubject *signal1 = [RACSubject subject];
RACSubject *signal2 = [RACSubject subject];
RACSignal *combineSignal = [RACSignal combineLatest: @[signal2, signal1] reduce:^id _Nullable (NSString *a, NSString * b){
return @(a.length && b.length);
}];
[combineSignal subscribeNext:^(id _Nullable x) {
NSLog(@"combine : %@",x);
}];
[signal2 sendNext: @"2"];
[signal1 sendNext: @""];
4.使用zip来等待多信号均发送数据后再按顺序压缩为元组
- 无需使用
sendCompleted
仍可接受到信号 - 只要参与压缩的信号都完成一轮数据发送,就会按照压缩顺序生成新的信号,接受所有参与压缩的信号发送来的数据
- 最终压缩得到的数据为元组
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"2222"];
return nil;
}];
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"1111"];
return nil;
}];
RACSignal *signal3 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"333"];
return nil;
}];
// 写法1 RACSignal *zipSignal = [signal1 zip: signal2];
RACSignal *zipSignal = [RACSignal zip: @[signal3, signal1, signal2]];
[zipSignal subscribeNext:^(id _Nullable x) {
NSLog(@"zipSignal : %@", x);
}];
5.使用lift_selector来实现多信号按顺序均发送完再执行某一信号
!!注意最后执行的方法必须要有参数,且数量跟先执行的信号量个数相同才可以
- (void)viewDidLoad {
[super viewDidLoad];
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号1发送"];
return nil;
}];
[signal1 subscribeNext:^(id _Nullable x) {
NSLog(@"signal1 : %@", x);
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号2发送"];
return nil;
}];
[signal2 subscribeNext:^(id _Nullable x) {
NSLog(@"signal2 : %@", x);
}];
RACSignal *lastSignal = [self rac_liftSelector: @selector(lastAction:secondP:) withSignals:signal2, signal1, nil];
[lastSignal subscribeNext:^(id _Nullable x) {
NSLog(@"lastSignal");
}];
}
- (void)lastAction:(id)firstP secondP:(id)secondP {
NSLog(@"lastAction : %@ , %@", firstP, secondP);
}
6.使用then来忽略前一个信号发送的值
忽略前一个信号发送的值,且等待前一个信号发送完成sendCompleted
后,才会连接后一个信号并接受他的发送值
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号2发送"];
return nil;
}];
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"信号1发送"];
[subscriber sendCompleted];
return nil;
}];
RACSignal *thenSignal = [signal1 then:^RACSignal * _Nonnull{
return signal2;
} ];
// 忽略了信号1发送的值,等信号1 completed后就会接受信号2的值
[thenSignal subscribeNext:^(id _Nullable x) {
NSLog(@"thenSignal : %@", x);
}];