ios之ReactiveCocoa框架的学习1

xiaoxiao2021-02-28  58

以下是几种简单的用法:(导入reactivecocoa框架的时候注意,只使用oc语言导入2.5版本以前,包括2.5版本,使用swift需要导入2.5版本以后)

// // ViewController.m // RAC05 // // Created by mac on 2018/5/2. // Copyright © 2018年 Gooou. All rights reserved. // #import "ViewController.h" @import ReactiveCocoa; @interface ViewController ()<UITextFieldDelegate> { RACCommand *command; } @property (strong, nonatomic) IBOutlet UITextField *userTextField; @property (strong, nonatomic) IBOutlet UITextField *pwdTextField; @property (strong, nonatomic) IBOutlet UIButton *loginBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //RACSignal必须是要有订阅者才能发送消息 RACSignal *signal=[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { //发送信号,由订阅者接收 [subscriber sendNext:@"发送信号"]; //不再发送信号,最好发送完成 [subscriber sendCompleted]; //取消订阅方法 return [RACDisposable disposableWithBlock:^{ //当信号发送完成或者发送错误,就会执行这个block NSLog(@"信号被销毁了"); }]; }]; //订阅信号,只有被订阅,信号量内部才会发送消息,执行sendnext方法 [signal subscribeNext:^(id x) { NSLog(@"订阅信号%@",x); }]; //=================================================================================================== //创建信号,只能先订阅才能发送 RACSubject *subject=[RACSubject subject]; //订阅信号 [subject subscribeNext:^(id x) { NSLog(@"第一个订阅者%@",x); }]; [subject subscribeNext:^(id x) { NSLog(@"第二个订阅者%@",x); }]; //发送消息 [subject sendNext:@"121212"]; //发送消息完成后,内部会自动取消订阅者 [subject sendCompleted]; //=================================================================================================== //可以先发送消息,也可以先订阅消息 RACReplaySubject *replaysubject=[RACReplaySubject subject]; //发送信号 [replaysubject sendNext:@"1"]; [replaysubject sendNext:@"2"]; //订阅信号 [replaysubject subscribeNext:^(id x) { NSLog(@"第一个订阅者接收到数据%@",x); }]; [replaysubject subscribeNext:^(id x) { NSLog(@"第二个订阅者接受到数据%@",x); }]; //=================================================================================================== //RAC集合 //元组 RACTuple *tuple=[RACTuple tupleWithObjectsFromArray:@[@"123",@"345",@1]]; NSString *first=tuple[0]; NSLog(@"%@",first); //数组 NSArray *arr=@[@"213",@"321",@1]; [arr.rac_sequence.signal subscribeNext:^(id x) { NSLog(@"高级写法遍历数组%@",x); }]; //字典 NSDictionary *dict=@{@"sex":@"女",@"name":@"打交道的",@"age":@18}; //转换成集合 [dict.rac_sequence.signal subscribeNext:^(id x) { RACTupleUnpack(NSString *key,NSString *value)=x; NSLog(@"%@ %@",key,value); }]; //=================================================================================================== //RACMulticastConnection用于相当一个信号,被多次订阅时,为了保证创建信号时,避免多次调用创建信号中的block,造成副作用,可以使用这个类处理。 //创建信号 RACSignal *signall=[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"发送请求"); [subscriber sendNext:@"1"];//发送消息,订阅者接受消息 return [RACDisposable disposableWithBlock:^{ }]; }]; //订阅信息-运行结果,会执行两遍发送请求,也就是每次订阅都会发送一次请求 [signall subscribeNext:^(id x) { NSLog(@"接受消息"); }]; [signall subscribeNext:^(id x) { NSLog(@"接受消息"); }]; //解决重复请求的问题 RACSignal *connectionSignal=[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { NSLog(@"发送请求"); [subscriber sendNext:@1]; return [RACDisposable disposableWithBlock:^{ }]; }]; //创建链接 RACMulticastConnection *connection=[connectionSignal publish]; //订阅信息 [connection.signal subscribeNext:^(id x) { NSLog(@"订阅者一信号"); }]; [connection.signal subscribeNext:^(id x) { NSLog(@"订阅者二信号"); }]; //连接,激活信号 [connection connect]; //=================================================================================================== //RACCommand:RAC中用于处理事件的类,可以把事件如何处理,事件中的数据如何传递,包装到这个类中,他可以很方便的监控事件的执行过程-使用场景:监听按钮点击,网络请求 //创建命令 RACCommand *command=[[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) { NSLog(@"执行命令"); //创建信号,用来传递数据 return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext:@"请求数据"]; //注意:数据传递完,最好用sendCompleted,这时命令才执行完 [subscriber sendCompleted]; return [RACDisposable disposableWithBlock:^{ NSLog(@"数据销毁"); }]; }]; }]; //强引用命令,,不要被销毁,否则接收不到消息 command=command; [command.executing subscribeNext:^(id x) { if ([x boolValue]==YES) { NSLog(@"当前正在执行"); }else{ NSLog(@"执行完成/没有执行"); } }]; //执行命令 [command execute:@1]; //=================================================================================================== //RAC的高级用法 RACSubject *signalOFSignal=[RACSubject subject]; RACSubject *signalA=[RACSubject subject]; //RACSignal *signalB=[RACSubject subject]; [signalOFSignal.switchToLatest subscribeNext:^(id x) { NSLog(@"%@",x); }]; //发送信号 [signalOFSignal sendNext:signalA]; [signalA sendNext:@1]; [signalA sendNext:@"BB"]; [signalA sendNext:@"11"]; } @end
转载请注明原文地址: https://www.6miu.com/read-2600288.html

最新回复(0)