组合
一、concat
按顺序组合发送信号 注意:每当一个信号发送结束后需执行完成发送方法才能进行下一个信号的发送
//组合!!
//创建信号!!
RACSignal * signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"发送请求A");
//发送数据
[subscriber sendNext:@"数据A"];
//注意:每当一个信号发送结束后需执行完成发送方法才能进行下一个信号的发送
[subscriber sendCompleted];
return nil;
}];
RACSignal * signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"发送请求B");
//发送数据
[subscriber sendNext:@"数据B"];
[subscriber sendCompleted];
return nil;
}];
RACSignal * signalC = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"发送请求C");
//发送数据
[subscriber sendNext:@"数据C"];
return nil;
}];
//concat:按顺序组合!!
//创建组合信号!!
RACSignal * concatSignal = [RACSignal concat:@[signalA,signalB,signalC]];
//订阅组合信号
[concatSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
Snip20181105_23.png
二、then
忽略掉第一个信号所有的值
//创建信号!!
RACSignal * signalA = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"发送请求A");
//发送数据
[subscriber sendNext:@"数据A"];
[subscriber sendCompleted];
return nil;
}];
RACSignal * signalB = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
NSLog(@"发送请求B");
//发送数据
[subscriber sendNext:@"数据B"];
[subscriber sendCompleted];
return nil;
}];
//then:忽略掉第一个信号所有的值!!
RACSignal * thenSignal = [signalA then:^RACSignal * _Nonnull{
return signalB;
}];
//订阅信号
[thenSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
Snip20181105_24.png
三、merge
根据发送的情况接受数据:当多个信号时谁先发送完成谁就会被先接收到数据
//创建信号
RACSubject * signalA = [RACSubject subject];
RACSubject * signalB = [RACSubject subject];
RACSubject * signalC = [RACSubject subject];
//组合信号
RACSignal * mergeSignal = [RACSignal merge:@[signalA,signalB,signalC]];
//订阅 -- 根据发送的情况接受数据!!
[mergeSignal subscribeNext:^(id _Nullable x) {
//任意一二信号发送内容就会来这个Block
NSLog(@"%@",x);
}];
//发送数据
[signalC sendNext:@"数据C"];
[signalA sendNext:@"数据A"];
[signalB sendNext:@"数据B"];
Snip20181106_25.png
四、combineLatest:将combineLatest:后面的数组中的信合打包成为一个新的信号。只有当数组中信号都成功发送过信号的时候打包后的信号才能正常执行订阅后的代码块。
//组合
//reduceBlock参数:根据组合的信号关联的 必须 一一对应!!
RACSignal * signal = [RACSignal combineLatest:@[_accountFiled.rac_textSignal,_pwdFiled.rac_textSignal] reduce:^id _Nullable(NSString * account,NSString * pwd){
//两个文本框的text是否有值!!
return @(account.length && pwd.length);
}];
RAC(_loginBtn,enabled) = signal;
Snip20181106_37.png
注意:combineLatest和merge的区别:前者是数组中信号都成功发送过信号的时候打包后的信号才能正常执行订阅后的代码块,后者是任意一个信号发送成功就可以正常执行订阅后的代码
五、zipWith:两个信号压缩!只有当两个信号同时发出信号内容,并且将内容合并成为一个元祖给你
//zipWith:两个信号压缩!只有当两个信号同时发出信号内容,并且将内容合并成为一个元祖给你
//创建信号
RACSubject * signalA = [RACSubject subject];
RACSubject * signalB = [RACSubject subject];
//压缩
RACSignal * zipSignal = [signalA zipWith:signalB];
//接受数据 和发送顺序无关!!
[zipSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
//发送数据
[signalB sendNext:@"B" ];
[signalA sendNext:@"A" ];
[signalB sendNext:@"B1"];
[signalA sendNext:@"A1"];
[signalB sendNext:@"B2"];
[signalA sendNext:@"A2"];
Snip20181106_26.png
Snip20181106_27.png
过滤
一、filter:就是过滤条件,只有满足这个条件,才能获取到内容
[[_textfiled.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
//value:源信号的内容
return [value length] > 5;
//返回值:就是过滤条件,只有满足这个条件,才能获取到内容
}] subscribeNext:^(NSString * _Nullable x) {
NSLog(@"%@",x);
}];
Snip20181106_28.png
二、ignore:忽略一些值(指定值)
//ignore:忽略
RACSubject * subject = [RACSubject subject];
//忽略一些值!!
RACSignal * ignoreSignal = [[[subject ignore:@"1"] ignore:@"2"] ignore:@"3"];
//订阅
[ignoreSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
//发送数据
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[subject sendNext:@"13"];
[subject sendNext:@"3"];
Snip20181106_29.png
三、1、take:指定拿前面的哪几条数据!!(从前往后)
RACSubject * subject = [RACSubject subject];
//take:指定拿前面的哪几条数据!!(从前往后)
//takeLast:指定拿前面的哪几条数据!!(从后往前)注意点:一定要写结束!!
[[subject take:2] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"2"];
[subject sendNext:@"3"];
[subject sendNext:@"1"];
[subject sendCompleted];
Snip20181106_31.png
2、takeLast:指定拿前面的哪几条数据!!(从后往前)注意点:一定要写结束!!
RACSubject * subject = [RACSubject subject];
//take:指定拿前面的哪几条数据!!(从前往后)
//takeLast:指定拿前面的哪几条数据!!(从后往前)注意点:一定要写结束!!
[[subject takeLast:2] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"2"];
[subject sendNext:@"3"];
[subject sendNext:@"1"];
[subject sendCompleted];
Snip20181106_32.png
3、takeUntil:直到你的标记信号发送数据的时候结束
RACSubject * subject = [RACSubject subject];
[[subject takeLast:2] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"2"];
[subject sendNext:@"3"];
[subject sendNext:@"1"];
[subject sendCompleted];
//专门做一个标记信号!!
RACSubject * signal = [RACSubject subject];
//take:指定拿前面的哪几条数据!!(从前往后)
//takeLast:指定拿前面的哪几条数据!!(从后往前)注意点:一定要写结束!!
//takeUntil:直到你的标记信号发送数据的时候结束!!!
[[subject takeUntil:signal] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"2"];
[signal sendCompleted];//标记信号!!
[subject sendNext:@"3"];
[subject sendNext:@"1"];
[subject sendCompleted];
Snip20181106_34.png
四、distinctUntilChanged:忽略重复数据
//1.创建信号
RACSubject * subject = [RACSubject subject];
//忽略掉重复数据
[[subject distinctUntilChanged] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
//请求回来
[subject sendNext:@"hmp"];
[subject sendNext:@"hmp"];
[subject sendNext:@"hmp ff"];
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[subject sendNext:@"2"];
Snip20181106_35.png
五、跳跃几个值
RACSubject * subject = [RACSubject subject];
//skip: 跳跃几个值
[[subject skip:2] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext:@"1"];
[subject sendNext:@"2"];
[subject sendNext:@"3"];
Snip20181106_36.png
网友评论