本片主要介绍RAC的方法和修饰符。
首先来说下什么是RAC,RAC给我们写代码带来了什么便捷。可能刚遇到的时候你会感觉这个东东太抽象了,入门好难,看着看着就没兴趣了。其实如果你进入它的世界,你会发现它的魅力,它会让你的代码写起来更舒服,你会喜欢上它。
RAC基本构造单元就是RACSignal,在它的眼里就是所有都是一个个信号在传递,你可以开始预先处理好运用逻辑并构建你的信息流,而不用必须等到事件发生(命令式).也就是说你的处理事件、一些逻辑都是提前就已经搞好了,只等待别人的订阅就可以收到这个信号。
那么在什么时候使用呢?简单点说就是原来需要委托, 回调 block, 通知, KVO, target/action 事件观察, 等,都可以用RAC信号来代替。信号会为了控制通过应用的信息流而获得所有这些异步方法(委托, 回调 block, 通知, KVO, target/action 事件观察, 等)并将它们统一到一个接口下.这只是直观理解. 不仅是这些, 因为信息会流过你的应用, 它还提供给你轻松转换/分解/合并/过滤信息的能力.
首先来看下RAC的相关方法:
1. RAC发送消息,并且绑定到控件,最基本的使用。
-(void)racSenderMessage {
//延迟2.0S 发送消息
RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"消息"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"一些处理事件");
}];
}] delay:2.0];
//将_userNameFeild的`text`属性与映射后的信号量的值绑定到一起
RAC(_userNameFeild , text) = [signal map:^id(id value) {
if ([value isEqualToString:@"消息"]) {
return @"成功收到";
}
return nil;
}];
}
2. RAC代理,使用rac_signalForSelector这个方法来获取代理信号.下边是调用alertview的代理方法
-(void)racProtocolMothel {
RACSignal *protocolSignal = [self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)];
[protocolSignal subscribeNext:^(id x) {
}];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
}
3. RAC通知
-(void)racNotification {
//接受通知并且处理
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"RAC_Notifaciotn" object:nil] subscribeNext:^(id x) {
// NSLog(@"notify.content = %@",notify.userInfo[@"content"]);
}];
//发出通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"RAC_Notifaciotn" object:nil userInfo:@{@"content" : @"i'm a notification"}];
}
//* 4 RAC信号拼接 concat是signal1 completed之后 signal2才能执行
-(void)racSignalLink {
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@(1)];
[subscriber sendCompleted];
return nil;
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@(2)];
[subscriber sendCompleted];
return nil;
}];
RACSignal* concatSignal = [RACSignal concat:@[signal1,signal2]];
[concatSignal subscribeNext:^(id value) {
NSLog(@"RAC信号拼接------value = %@",value);
}];
//或者
// [[signal1 concat:signal2] subscribeNext:^(id value) {
// NSLog(@"RAC信号拼接------value = %@",value);
// }];
}
//* 5 RAC信号合并
-(void)racSignalMerge {
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"AA"];
[subscriber sendCompleted];
return nil;
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"BB"];
[subscriber sendCompleted];
return nil;
}];
//合并操作
RACSignal* mergeSignal = [RACSignal merge:@[signal1,signal2]];
[mergeSignal subscribeNext:^(id x) {
NSLog(@"RAC信号合并------我喜欢: %@",x);
}];
//或者
// [[signal1 merge:signal2] subscribeNext:^(id x) {
// NSLog(@"RAC信号合并------我喜欢: %@",x);
// }];
}
//* 6 RAC信号组合(取信号量的最后发送的对象)
-(void)racSignalCombine {
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"SS"];
[subscriber sendNext:@"AA"];
[subscriber sendCompleted];
return nil;
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"BB"];
[subscriber sendCompleted];
return nil;
}];
//combineLatest 将数组中的信号量发出的最后一个object 组合到一起
[[RACSignal combineLatest:@[signal1,signal2]] subscribeNext:^(id x) {
RACTupleUnpack(NSString *signal1_Str, NSString *signal2_Str) = (RACTuple *)x;
NSLog(@"RAC信号组合------我就是 %@ %@",signal1_Str,signal2_Str);
}];
//会注意收到 组合方法后还可以跟一个Block /** + (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals reduce:(id (^)())reduceBlock */
/*
reduce这个Block可以对组合后的信号量做处理
*/
//我们还可以这样使用
RACSignal * combineSignal =[RACSignal combineLatest:@[signal1,signal2] reduce:^(NSString *signal1_Str, NSString *signal2_Str){
return [signal1_Str stringByAppendingString:signal2_Str];
}];
[combineSignal subscribeNext:^(id x) {
NSLog(@"RAC信号组合(Reduce处理)------我喜欢 %@ 的",x);
}];
}
//* 7 RAC信号组合(取信号量的最开始发送的对象)全部获取才会返回
/*当且仅当signalA和signalB同时都产生了值的时候,一个value才被输出,signalA和signalB只有其中一个有值时会挂起等待另一个的值,所以输出都是一对值(RACTuple)),当signalA和signalB只要一个先completed,RACStream也解散。*/
-(void)racSignalZIP {
RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"AA"];
[subscriber sendNext:@"BB"];
[subscriber sendCompleted];
return nil;
}];
RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"CC"];
[subscriber sendCompleted];
return nil;
}];
[[RACSignal zip:@[signal1,signal2]] subscribeNext:^(id x) {
RACTupleUnpack(NSString *signal1_Str, NSString *signal2_Str) = (RACTuple *)x;
NSLog(@"RAC信号压缩------我是 %@的 %@的 ",signal1_Str, signal2_Str);
}];
}
//* 8 RAC信号过滤
-(void)racSignalFilter {
[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@(19)];
[subscriber sendNext:@(12)];
[subscriber sendNext:@(20)];
[subscriber sendCompleted];
return nil;
}] filter:^BOOL(id value) {
NSNumber *numberValue = value;
if(numberValue.integerValue < 18) {
//18禁
NSLog(@"RAC信号过滤------FBI Warning~");
}
return numberValue.integerValue > 18;
}] subscribeNext:^(id x) {
NSLog(@"RAC信号过滤------年龄:%@",x);
}];
}
// 9 RAC信号传递(传递数值,前后信号有联系)
-(void)racSignalPass {
[[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"老板向我扔过来一个Star"];
return nil;
}] flattenMap:^RACStream *(id value) {
NSLog(@"RAC信号传递flattenMap1------%@",value);
RACSignal *tmpSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:[NSString stringWithFormat:@"%@----我向老板扔回一块板砖",value]];
return nil;
}];
return tmpSignal;
}] flattenMap:^RACStream *(id value) {
NSLog(@"RAC信号传递flattenMap2------%@",value);
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:[NSString stringWithFormat:@"%@---我跟老板正面刚~,结果可想而知",value]];
return nil;
}];
}] subscribeNext:^(id x) {
NSLog(@"RAC信号传递last------%@",x);
}];
}
//* 10 RAC信号传递(不传递数值)
-(void)racSignalQueue {
//与信号传递类似,不过使用 `then` 表明的是秩序,没有传递value
[[[[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"RAC信号串------我先来");
[subscriber sendCompleted];
return nil;
}] then:^RACSignal *{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"RAC信号串------我第二");
[subscriber sendCompleted];
return nil;
}];
}] then:^RACSignal *{
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"RAC信号串------我第三");
[subscriber sendCompleted];
return nil;
}];
}] subscribeNext:^(id x) {
NSLog(@"RAC信号串------Over");
}];
}
//* 11 RAC_Command介绍
-(void)racCommandDemo {
RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"racCommandDemo------");
[subscriber sendCompleted];
return nil;
}];
}];
//命令执行
[command execute:nil];
}
上边是部分RAC方法的理解。