一、combineLatest
RACSubject *signal1 = [RACSubject subject];
RACSubject *signal2 = [RACSubject subject];
[[RACSignal combineLatest:@[signal1, signal2]] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"combineLatest 回调 %@", x);
}];
[signal1 sendNext:@"1"];
[signal2 sendNext:@"A"];
[signal1 sendNext:@"2"];
[signal2 sendNext:@"B"];
[signal1 sendNext:@"3"];
[signal2 sendNext:@"C"];
// 控制台输出
combineLatest 回调 <RACTuple: 0x1c0012000> (
1,
A
)
combineLatest 回调 <RACTuple: 0x1c0012070> (
2,
A
)
combineLatest 回调 <RACTuple: 0x1c00120b0> (
2,
B
)
combineLatest 回调 <RACTuple: 0x1c0012120> (
3,
B
)
combineLatest 回调 <RACTuple: 0x1c0012160> (
3,
C
)
signal1: 1 2 3
signal2: A B C
result: A1 A2 B2 B3 C3
>>>>>>>> time >>>>>>>>
combineLatest
用于合并多个信号,当所以信号都收到消息,才会触发回调。回调内容为各个信号最新信息的元组
可用于对 提交按钮是否禁用 的控制,例如登录页面,当账号密码信号全部满足条件时触发 并且可用 combineLatest
的变形方法 + (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals reduce:(RACGenericReduceBlock)reduceBlock
利用reduceBlock
对回调数据过滤返回新的内容
二、zip
RACSubject *signal1 = [RACSubject subject];
RACSubject *signal2 = [RACSubject subject];
[[RACSignal zip:@[signal1, signal2]] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"zip 回调 %@", x);
}];
[signal1 sendNext:@"1"];
[signal2 sendNext:@"A"];
[signal1 sendNext:@"2"];
[signal2 sendNext:@"B"];
[signal1 sendNext:@"3"];
[signal2 sendNext:@"C"];
// 控制台输出
zip 回调 <RACTuple: 0x1c401ea00> (
1,
A
)
zip 回调 <RACTuple: 0x1c401ea80> (
2,
B
)
zip 回调 <RACTuple: 0x1c401eb00> (
3,
C
)
signal1: 1 2 3
signal2: A B C
result: A1 B2 C3
>>>>>>>> time >>>>>>>>
zip
效果与combineLatest
类似,都是用来合并多个信号,当所有信号都收到消息才会回调, 不同的是combineLatest
每次会返回各个信号最后一条消息,而zip
是按顺序取出各个信号然后合并发出的.也就是说,各个信息的第一条消息进行合并回调,第二条消息合并回调,以此类推,只有各个信息同序号都有消息时才会回调。
三、merge
RACSubject *signal1 = [RACSubject subject];
RACSubject *signal2 = [RACSubject subject];
[[RACSignal merge:@[signal1, signal2, signal3]] subscribeNext:^(RACTuple * _Nullable x) {
NSLog(@"merge 回调 %@", x);
}];
[signal1 sendNext:@"1"];
[signal2 sendNext:@"2"];
// 控制台输出
merge 回调 1
merge 回调 2
merge
用于统一管理多个信号回调,每个信号收到消息时,都会进行的回调