20180119-01-RACSignal的基础
阅读原文时间:2023年07月13日阅读:1

一.获取一个信号的方式

1.单元信号

RACSignal \*signal1 = \[RACSignal return:@"Some Value"\];  
RACSignal \*signal2 = \[RACSignal error:\[NSError errorWithDomain:NSCocoaErrorDomain code: userInfo:nil\]\];  
RACSignal \*signal3 = \[RACSignal empty\];  
RACSignal \*signal4 = \[RACSignal never\];

  2.动态信号

RACSignal *signal5 = [RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
[subscriber sendNext:@];
[subscriber sendNext:@];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"信号已销毁");
}];
}];

  3.cocoa桥接

RACSignal *signal6 = [self.view rac_signalForSelector:@selector(setFrame:)];
RACSignal *signal7 = [self.btn rac_signalForControlEvents:UIControlEventTouchUpInside];
RACSignal *signal8 = [self.view rac_willDeallocSignal];
RACSignal *singal9 = RACObserve(self.view, backgroundColor)

  4.信号变换

RACSignal *signall10 = [signal1 map:^id _Nullable(id _Nullable value) {
return [value substringFromIndex:];
}];

  5.序列变换

RACSequence *sequence = @[@,@,@].rac_sequence;
RACSignal *signal11 = sequence.signal;

二.订阅一个信号的方式

1.订阅方法

[signal11 subscribeNext:^(id _Nullable x) {
NSLog(@"next value is %@",x);
} error:^(NSError * _Nullable error) {
NSLog(@"Ops Get some error:%@",error);
} completed:^{
NSLog(@"It finish success");
}];

  2.绑定

RAC(self.view,backgroundColor) = signal9;

  3.Cocoa桥接

[self.view rac_liftSelector:@selector(convertPoint:toView:) withSignals:signal1,signal2, nil];
[self.view rac_liftSelector:@selector(convertPoint:toView:) withSignalsFromArray:@[signal1,signal2]];
[self.view rac_liftSelector:@selector(convertPoint:toView:) withSignalOfArguments:signal5];

  4.订阅过程

RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id _Nonnull subscriber) {
[subscriber sendNext:@];
[subscriber sendNext:@];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"信号销毁");
}];
}];

RACDisposable *disposable = [signal subscribeNext:^(id _Nullable x) {
NSLog(@"next value is %@",x);
} error:^(NSError * _Nullable error) {
NSLog(@"Ops Get some error:%@",error);
} completed:^{
NSLog(@"It finish success");
}];
[disposable dispose];

三.信号的变换和组合

四.RACSignal的各类操作

单个信号的变换