上篇RAC入门的文章相信大家都已经比较了解了,下面就给大家介绍RAC比较高级的东西。
1.创建信号
// RACSignal使用步骤:
// 1.创建信号 + (RACSignal *)createSignal:(RACDisposable * (^)(id<RACSubscriber> subscriber))didSubscribe
// 2.订阅信号,才会激活信号. - (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock
// 3.发送信号 - (void)sendNext:(id)value
// RACSignal底层实现:
// 1.创建信号,首先把didSubscribe保存到信号中,还不会触发。
// 2.当信号被订阅,也就是调用signal的subscribeNext:nextBlock
// 2.2 subscribeNext内部会创建订阅者subscriber,并且把nextBlock保存到subscriber中。
// 2.1 subscribeNext内部会调用siganl的didSubscribe
// 3.siganl的didSubscribe中调用[subscriber sendNext:@1];
// 3.1 sendNext底层其实就是执行subscriber的nextBlock
// 1.创建信号
RACSignal *siganl = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// block调用时刻:每当有订阅者订阅信号,就会调用block。
// 2.发送信号
[subscriber sendNext:@1];
// 如果不在发送数据,最好发送信号完成,内部会自动调用[RACDisposable disposable]取消订阅信号。
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
// block调用时刻:当信号发送完成或者发送错误,就会自动执行这个block,取消订阅信号。
// 执行完Block后,当前信号就不在被订阅了。
NSLog(@"信号被销毁");
}];
}];
// 3.订阅信号,才会激活信号.
[siganl subscribeNext:^(id x) {
// block调用时刻:每当有信号发出数据,就会调用block.
NSLog(@"接收到数据:%@",x);
}];
2.信号的处理:
filter:过滤前缀包含“mei”的字符串
[[mytextField.rac_textSignal filter:^BOOL(NSString *value) {
return [value hasPrefix:@"mei"];
}] subscribeNext:^(NSString *value) {
NSLog(@"This value has prefix `mei` : %@", value);
}];
ignore:忽略前缀包含“mei”的字符串
[[mytextField.rac_textSignal ignore:@"mei"] subscribeNext:^(NSString *value) {
NSLog(@"`mei` could never appear : %@", value);
}];
take:取前两个
[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendCompleted];
return nil;
}] take:2] subscribeNext:^(id x) {
NSLog(@"only 1 and 2 will be print: %@", x);
}];
skip:跳过第一个
[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"1"];
[subscriber sendNext:@"2"];
[subscriber sendNext:@"3"];
[subscriber sendCompleted];
return nil;
}] skip:1] subscribeNext:^(id x) {
NSLog(@"only 2 and 3 will be print: %@", x);
}];
throttle:节流
可能大家还是不理解,我给大家举个例子吧,我们公司曾经有一个需求,当用户再输入框输入的文本发生改变的时候可以请求数据,做到及时搜索的功能。就像百度现在的搜索体验。做到这个如果不考虑服务器压力其实没那么难,但是我们是追求完美的人,怎么能不考虑服务器性能呢。那么就有一个问题,一旦输入框发生改变我们就要从服务器获取数据吗,当然不能这样,这里我们如果有一个时间延迟那就最好了,那就是节流。
这里再介绍一下switchToLatest,当我们搜索的时候下一个搜索已经开始了可能上一个搜索还没返回结果,我们就没必要开启上一个搜索了,肯定是去最新的吧。所以switchToLatest就是这样的一个功能。好了,下面就是两者结合给我们的搜索优化的一段代码
[[[[[[textField.rac_textSignal throttle:0.3]distinctUntilChanged]ignore:@""] map:^id(id value) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
// network request
[subscriber sendNext:value];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
// cancel request
}];
}];
}]switchToLatest] subscribeNext:^(id x) {
NSLog(@"x = %@",x);
}];
repeat:重复
[[[[[RACSignal createSignal:^RACDisposable *(id subscriber) {
[subscriber sendNext:@"rac"];
[subscriber sendCompleted];
return nil;
}]delay:1]repeat]take:10] subscribeNext:^(id x) {
NSLog(@"x = %@",x);
} completed:^{
NSLog(@"完成");
}];
merge:合并 同时订阅,各自执行
RACSignal * signalA = [RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"A");
[subscriber sendNext:@"a"];
[subscriber sendCompleted];
});
return nil;
}];
RACSignal * signalB = [RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"B");
[subscriber sendNext:@"b"];
[subscriber sendCompleted];
});
return nil;
}];
[[RACSignal merge:@[signalA, signalB]]subscribeNext:^(id x) {
NSLog(@"x = %@",x);
}];
concat: 一个异步请求完成后,再启动另一个
RACSignal * signalA = [RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"a");
[subscriber sendNext:@"a"];
[subscriber sendCompleted];
});
return nil;
}];
RACSignal * signalB = [RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"b");
[subscriber sendNext:@"b"];
[subscriber sendCompleted];
});
return nil;
}];
[[signalA concat:signalB]subscribeNext:^(id x) {
NSLog(@"x=%@",x);
}];
zipWith:/combineLatest: 多个异步请求都完成后,再做某件事
RACSignal * signalA = [RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"a");
[subscriber sendNext:@"a"];
[subscriber sendCompleted];
});
return nil;
}];
RACSignal * signalB = [RACSignal createSignal:^RACDisposable *(id subscriber) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"b");
[subscriber sendNext:@"b"];
[subscriber sendCompleted];
});
return nil;
}];
[[signalA zipWith:signalB]subscribeNext:^(id x) {
NSLog(@"x = %@",x);
}];
RAC的一些宏
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:button];
button.frame = CGRectMake(100, 100, 180, 40);
RAC(button, backgroundColor) = [RACObserve(button, selected) map:^UIColor *(NSNumber * selected) {
return [selected boolValue] ? [UIColor redColor] : [UIColor greenColor];
}];
[[button rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(UIButton * btn) {
btn.selected = !btn.selected;
}];
RAC(showLabel, text) = mytextField.rac_textSignal;
RAC(showLabel, text) = [[[mytextField.rac_textSignal
startWith:@"不足3个字"] // startWith 一开始返回的初始值
filter:^BOOL(NSString *value) { // filter使满足条件的值才能传出
return value.length > 3;
}]map:^id(id value) {
// map将一个值转化为另一个值输出
return [value isEqualToString:@"meiqing"] ? @"验证成功!" : value;
}];
如果觉得文章还不错,可以打赏哦。
微博账号:梅嘉庆(点击关注)