ReactiveCocoa 在实际项目中的使用(一)基本操作
1、merge 操作
merge 是合并的意思,将两个或者多个信号合并,两个信号发送的 next 会汇聚到一起发送到合并后的信号的 next 事件中。两个信号都发送了完成事件后合并后的信号才会发送完成事件。
NSArray *numArray = @[@1, @2, @3, @4, @5];// 创建一个数字数组
RACSignal *numSignal = [numArray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler];
//数字数组转换成信号
NSArray *stringArrray = @[@"a", @"b", @"c", @"d", @"e", @"f"]; // 创建字符串数组
RACSignal *stringSignal = [stringArrray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler]; // 字符串数组转成信号
[[numSignal merge:stringSignal] subscribeNext:^(id _Nullable x) { //合并两个信号
NSLog(@"%@",x);
} completed:^{
NSLog(@"%@",@"done");
}];
Result:
2017-08-21 16:23:18.170228+0800 ReactiveCocoaDemo[356:50482] 1
2017-08-21 16:23:18.170587+0800 ReactiveCocoaDemo[356:50482] a
2017-08-21 16:23:18.170805+0800 ReactiveCocoaDemo[356:50482] 2
2017-08-21 16:23:18.171007+0800 ReactiveCocoaDemo[356:50482] b
2017-08-21 16:23:18.171214+0800 ReactiveCocoaDemo[356:50482] 3
2017-08-21 16:23:18.171413+0800 ReactiveCocoaDemo[356:50482] c
2017-08-21 16:23:18.171614+0800 ReactiveCocoaDemo[356:50482] 4
2017-08-21 16:23:18.171812+0800 ReactiveCocoaDemo[356:50482] d
2017-08-21 16:23:18.172032+0800 ReactiveCocoaDemo[356:50482] 5
2017-08-21 16:23:18.172377+0800 ReactiveCocoaDemo[356:50482] e
2017-08-21 16:23:18.172738+0800 ReactiveCocoaDemo[356:50482] f
2017-08-21 16:23:18.173014+0800 ReactiveCocoaDemo[356:50482] done
2、zipWith 操作
对几个信号发送的 next 事件进行压缩合并
NSArray *numArray = @[@1, @2, @3, @4, @5];// 创建一个数字数组
RACSignal *numSignal = [numArray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler];
//数字数组转换成信号
NSArray *stringArrray = @[@"a", @"b", @"c", @"d", @"e", @"f"]; // 创建字符串数组
RACSignal *stringSignal = [stringArrray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler]; // 字符串数组转成信号
[[[numSignal zipWith:stringSignal] //压缩两个信号
reduceEach:^(NSNumber *num, NSString *string){ // 对返回的数据进行解压
return [NSString stringWithFormat:@"%@%@", num, string];
}] subscribeNext:^(id _Nullable x) { //合并两个信号
NSLog(@"%@",x);
} completed:^{
NSLog(@"%@",@"done");
}];
Result:
2017-08-21 16:27:21.261630+0800 ReactiveCocoaDemo[362:51836] 1a
2017-08-21 16:27:21.262515+0800 ReactiveCocoaDemo[362:51836] 2b
2017-08-21 16:27:21.263100+0800 ReactiveCocoaDemo[362:51836] 3c
2017-08-21 16:27:21.263647+0800 ReactiveCocoaDemo[362:51836] 4d
2017-08-21 16:27:21.264318+0800 ReactiveCocoaDemo[362:51836] 5e
2017-08-21 16:27:21.264689+0800 ReactiveCocoaDemo[362:51836] done
3、combineLatest 操作
取联合的多个信号中的最新出现的值发送出来
NSArray *numArray = @[@1, @2, @3, @4, @5];// 创建一个数字数组
RACSignal *numSignal = [numArray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler];
//数字数组转换成信号
NSArray *stringArrray = @[@"a", @"b", @"c", @"d"]; // 创建字符串数组
RACSignal *stringSignal = [stringArrray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler]; // 字符串数组转成信号
[[[RACSignal combineLatest:@[numSignal, stringSignal]] reduceEach:^(NSNumber *num, NSString *str){ // 联合两个信号中最新的值传入 reduceEach处理
return [NSString stringWithFormat:@"%@%@",num, str]; //拼接两个value
}]
subscribeNext:^(NSString *x) {
NSLog(@"%@",x);
} completed:^{
NSLog(@"done");
}];
Result:
2017-08-21 16:16:06.597646+0800 ReactiveCocoaDemo[348:48433] 1a
2017-08-21 16:16:06.598708+0800 ReactiveCocoaDemo[348:48433] 2a
2017-08-21 16:16:06.599337+0800 ReactiveCocoaDemo[348:48433] 2b
2017-08-21 16:16:06.599928+0800 ReactiveCocoaDemo[348:48433] 3b
2017-08-21 16:16:06.600483+0800 ReactiveCocoaDemo[348:48433] 3c
2017-08-21 16:16:06.601214+0800 ReactiveCocoaDemo[348:48433] 4c
2017-08-21 16:16:06.601999+0800 ReactiveCocoaDemo[348:48433] 4d
2017-08-21 16:16:06.602748+0800 ReactiveCocoaDemo[348:48433] 5d
2017-08-21 16:16:06.603451+0800 ReactiveCocoaDemo[348:48433] done
4、concat 操作
concat 是连接两个信号的操作符。它必须等待前一个信号发送了 completed 信号后才会发送后一个信号的next 事件和 completed 事件
NSArray *numArray = @[@1, @2, @3, @4, @5];// 创建一个数字数组
RACSignal *numSignal = [numArray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler];
//数字数组转换成信号
NSArray *stringArrray = @[@"a", @"b", @"c", @"d", @"e", @"f"]; // 创建字符串数组
RACSignal *stringSignal = [stringArrray.rac_sequence signalWithScheduler:RACScheduler.mainThreadScheduler]; // 字符串数组转成信号
[[numSignal concat:stringSignal] // 连接两个信号
subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
} completed:^{
NSLog(@"%@",@"done");
}];
Resilt:
2017-08-21 17:23:18.741757+0800 ReactiveCocoaDemo[418:66579] 1
2017-08-21 17:23:18.742078+0800 ReactiveCocoaDemo[418:66579] 2
2017-08-21 17:23:18.742264+0800 ReactiveCocoaDemo[418:66579] 3
2017-08-21 17:23:18.742441+0800 ReactiveCocoaDemo[418:66579] 4
2017-08-21 17:23:18.742616+0800 ReactiveCocoaDemo[418:66579] 5
2017-08-21 17:23:18.743216+0800 ReactiveCocoaDemo[418:66579] a
2017-08-21 17:23:18.743409+0800 ReactiveCocoaDemo[418:66579] b
2017-08-21 17:23:18.743582+0800 ReactiveCocoaDemo[418:66579] c
2017-08-21 17:23:18.743753+0800 ReactiveCocoaDemo[418:66579] d
2017-08-21 17:23:18.743924+0800 ReactiveCocoaDemo[418:66579] e
2017-08-21 17:23:18.744094+0800 ReactiveCocoaDemo[418:66579] f
2017-08-21 17:23:18.744311+0800 ReactiveCocoaDemo[418:66579] done
网友评论