学习RAC先从熟悉简单类的使用开始,RACSignal信号类,使用方法:
(1)createSignal创建信号类
(2)订阅信号,subscribeNext,如果没有订阅信号,为冷信号,订阅后才能成为热信号
(3)sendnext 发送信号
工作流程:
(1)创建信号,保存了createSignal参数block,命名为block1
(2)订阅信号,调用subscribeNext,内部创建了订阅者,并调用block1,并保存了subscribeNext参数block,命名为block2,
(3)发送信号,sendNext,本质上是调用的订阅者的block,即block2
-(void)racSignalTest
{
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@1];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"信号被摧毁");
}];
}];
[signal subscribeNext:^(id x) {
NSLog(@"%@,type;%@",x,NSStringFromClass(object_getClass(x)));
}];
[signal subscribeNext:^(id x) {
NSLog(@"%@,type;%@",x,NSStringFromClass(object_getClass(x)));
}];
}
RACSubject:信号提供者,自己可以充当信号,又能发送信号。
创建方法:
(1)创建RACSubject
(2)订阅信号
(3)发送信号
工作流程:
(1)订阅信号时,内部保存了订阅者,和订阅者响应block
(2)当发送信号的,遍历订阅者,调用订阅者的nextBlock
注:如果订阅信号,必须在发送信号之前订阅信号,不然收不到信号,这也有别于RACReplaySubject
-(void)racSubjectTest
{
RACSubject *subject = [RACSubject subject];
[subject subscribeNext:^(id x) {
NSLog(@"1 %@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
[subject subscribeNext:^(id x) {
NSLog(@"2 %@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
[subject sendNext:@1];
[subject subscribeNext:^(id x) {
NSLog(@"3 %@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
}
RACSubject替代代理
第一个控制器,点击按钮进入第二个控制器
- (IBAction)butClicked:(id)sender {
CustumViewController *towVc = [[CustumViewController alloc] init];
towVc.subjec = [RACSubject subject];
[towVc.subjec subscribeNext:^(id x) {
NSLog(@"被通知了:%@",x);
}];
[self.navigationController pushViewController:towVc animated:YES];
}
第二个控制器,点击按钮通知第一个控制器
- (IBAction)btnClicked:(id)sender {
if (self.subjec) {
[self.subjec sendNext:@1];
}
}
RACReplaySubject 创建方法:
(1)创建RACSubject
(2)订阅信号
(3)发送信号
工作流程:
(1)订阅信号时,内部保存了订阅者,和订阅者响应block
(2)当发送信号的,遍历订阅者,调用订阅者的nextBlock
(3)发送的信号会保存起来,当订阅者订阅信号的时,会将之前保存的信号,一个一个作用于新的订阅者,保存信号的容量由capacity决定,这也是有别有RACSubject的
-(void)RACReplaySubject
{
RACReplaySubject *replaySubject = [RACReplaySubject subject];
[replaySubject subscribeNext:^(id x) {
NSLog(@"1 %@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
[replaySubject subscribeNext:^(id x) {
NSLog(@"1 %@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
[replaySubject sendNext:@1];
[replaySubject subscribeNext:^(id x) {
NSLog(@"3 %@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
}
RACSequence
-(void)RACSequenceTest
{
NSArray *arr = @[@1,@2,@3,@4,@5,@6];
[arr.rac_sequence.signal subscribeNext:^(id x) {
NSLog(@"x:%@",x);
}];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"jtd",@"name",@"man",@"sex",@"jx",@"jg", nil];
[dict.rac_sequence.signal subscribeNext:^(id x) {
RACTupleUnpack(NSString *key,NSString *value) = x;
NSLog(@"key:%@,value:%@",key,value);
}];
NSDictionary *dict1 = @{@"key1":@"value1",@"key2":@"value2"};
NSDictionary *dict2 = @{@"key1":@"value1",@"key2":@"value2"};
NSArray *dictArr = @[dict1,dict2];
[dictArr.rac_sequence.signal subscribeNext:^(id x) {
NSLog(@"x:%@,type:%@",x,NSStringFromClass(object_getClass(x)));
}];
}
RAC正在学习路上......
网友评论