1.使用map映射来改变返回值类型
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"哈哈"];
[subscriber sendCompleted];
return nil;
}];
RACSignal *mapSigmal = [signal map:^id _Nullable(id _Nullable value) {
return @([value isEqualToString: @"哈哈"]);
}];
[mapSigmal subscribeNext:^(id _Nullable x) {
NSLog(@"结果是: %@", x);
}];
2.使用flattenMap来映射新的信号
根据前一个信号的参数创建一个新的信号!
//1.创建信号
RACSubject *subject = [RACSubject subject];
//2.绑定信号
RACSignal *flattenSignal = [subject flattenMap:^RACSignal *(id value) {
//value:源信号发送的内容
value = [NSString stringWithFormat:@"ABC%@", value];
//返回的信号:用来包装成修改内容值
return[RACReturnSignal return:value];
}];
//flattenMap中返回的是什么信号,订阅的就是什么信号
//订阅信号
[flattenSignal subscribeNext:^(id x) {
NSLog(@"%@", x);
}];
//发送数据
[subject sendNext: @"D"];
3.使用filter来过滤掉信号订阅得到的具体值
RACSignal * signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"hah"];
[subscriber sendNext: @"xixi"];
return nil;
}];
[[signal filter:^BOOL(id _Nullable value) {
return [value isEqualToString: @"xixi"];
}] subscribeNext:^(id _Nullable x) {
NSLog(@"subscribeNext %@",x);
}];
4.使用bind来绑定信号,在绑定信号中处理得到的原始数据
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext: @"数据"];
return nil;
}];
RACSignal *bindSignal = [signal bind:^RACSignalBindBlock _Nonnull{
return ^RACSignal * ( id value, BOOL *stop) {
//只要原信号发送数据,就会执行此block得到原始数据
//可以在此处对数据进行加工,来返回加工后的数据
return [RACReturnSignal return: value];
};
}];
[bindSignal subscribeNext:^(id _Nullable x) {
NSLog(@"bindSignal 加工过的数据: %@", x);
}];
[signal subscribeNext:^(id _Nullable x) {
NSLog(@"signal 原始数据: %@", x);
}];
5.使用skip来跳过信号的第几次数据
RACSubject *signal = [RACSubject subject];
//跳过第一次发送的数据
[[signal skip: 1] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[signal sendNext: @"1"];
[signal sendNext: @"2"];
[signal sendNext: @"3"];
skipUntil
一直跳过直到返回结果为YES:
RACSubject *signal = [RACSubject subject];
[[signal skipUntilBlock:^BOOL(id _Nullable x) {
NSLog(@"%@",x);
return ([x integerValue] == 2);
}] subscribeNext:^(id _Nullable x) {
NSLog(@"final :%@",x);
}];
[signal sendNext: @"1"];
[signal sendNext: @"2"];
[signal sendNext: @"3"];
skipWhile
一直跳,直到block为NO
6.使用ignore忽略具体指值
RACSubject *signal = [RACSubject subject];
[[signal ignore: @"1"] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];;
[signal sendNext: @"1"];
[signal sendNext: @"2"];
[signal sendNext: @"3"];
7.使用take来获取前几次值
RACSubject *signal = [RACSubject subject];
[[signal take: 2] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[signal sendNext: @"1"];
[signal sendNext: @"2"];
[signal sendNext: @"3"];
takeUntilBlock
一直取值直到block返回YES时停止取值
RACSubject *signal = [RACSubject subject];
[[signal takeUntilBlock:^BOOL(id _Nullable x) {
return [x isEqualToString: @"2"];
}] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[signal sendNext: @"1"];
[signal sendNext: @"2"];
[signal sendNext: @"3"];
takeWhile
上面的反向逻辑,对于每个next值,block返回 YES时才取值
8.使用distinctUntilChange忽略重复值
RACSubject *subject = [RACSubject subject];
[[subject distinctUntilChanged] subscribeNext:^(id _Nullable x) {
NSLog(@"%@",x);
}];
[subject sendNext: @"11"];
[subject sendNext: @"11"];
[subject sendNext: @"22"];
[subject sendNext: @"33"];