RACSignal
RACSignal
继承自RACStream
,对该抽象类的方法进行了重写实现
return
方法
+ (RACSignal *)return:(id)value {
return [RACReturnSignal return:value];
}
/**
RACReturnSignal *signal = [[self alloc] init];
signal->_value = value;
*/
-bind
方法
/**
实现过程
1. 订阅原始信号值
2. 当原信号发送值时,使用bindblock对其进行转换
3. 如果bindblock返回一个信号,订阅该信号,并在收到该信号值时,将所有值传递给接收者subscriber
4. 如果bindblock请求终止就向原信号发出`-sendError:`消息
5. 当所有信号完成,向订阅者发送`-sendCompleted`
6. 无论何时 当信号发生错误,向订阅者发送-sendError: 消息
*/
- (RACSignal *)bind:(RACSignalBindBlock (^)(void))block
-bind
方法在原始信号每次发出消息时,执行RACSignalBindBlock
对其进行变换生成一个新的信号
示例:
//示例1:
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
[subscriber sendNext:@1];
[subscriber sendNext:@2];
[subscriber sendNext:@3];
[subscriber sendNext:@4];
[subscriber sendCompleted];
return nil;
}];
RACSignal *bindSignal = [signal bind:^RACSignalBindBlock _Nonnull{
return ^(NSNumber *value, BOOL *stop) {
//简单的利用bind将 原始信号的值进行了一个变换
value = @(value.integerValue * value.integerValue);
return [RACSignal return:value];
};
}];
[signal subscribeNext:^(id _Nullable x) {
NSLog(@"signal: %@", x);
}];
[bindSignal subscribeNext:^(id _Nullable x) {
NSLog(@"bindSignal: %@", x);
}];
-concat方法
- (RACSignal *)concat:(RACSignal *)signal {
return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
[self subscribeNext:^(id x) {
[subscriber sendNext:x];
} error:^(NSError *error) {
[subscriber sendError:error];
} completed:^{
[signal subscribe:subscriber];
}];
}] setNameWithFormat:@"[%@] -concat: %@", self.name, signal];
}
当原始信号completed
后 subcribe contact的信号
-zipWith方法
- (RACSignal *)zipWith:(RACSignal *)signal
将两个信号值进行zip 即每次发出一个为RACTuple
值为[signal1value, signal2value]的值 当其中一个error或者completed即停止zipSignal
信号的创建
RACSignal的createSignal
方法
//RACSignal
+ (RACSignal *)createSignal:(RACDisposable * (^)(id<RACSubscriber> subscriber))didSubscribe {
return [RACDynamicSignal createSignal:didSubscribe];
}
//RACDynamicSignal
+ (RACSignal *)createSignal:(RACDisposable * (^)(id<RACSubscriber> subscriber))didSubscribe {
RACDynamicSignal *signal = [[self alloc] init];
signal->_didSubscribe = [didSubscribe copy];
return [signal setNameWithFormat:@"+createSignal:"];
}
signal保存了didSubscribe
并在每次有订阅者订阅当前信号时 都会执行一遍,向订阅者发送消息
RACSignal类簇
RACSignal
拥有不同功能的子类
RACDynamicSignal
:用于+ createSignal:
RACEmptySignal
用于+ empty
+ (RACSignal *)empty { return [[[self alloc] init] setNameWithFormat:@"+empty"];
}
- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
return [RACScheduler.subscriptionScheduler schedule:^{
[subscriber sendCompleted];
}];
}RACReturnSignal
用于+ return
+ (RACSignal *)return:(id)value { RACReturnSignal *signal = [[self alloc] init];
signal->_value = value;
return signal;
}
//将value简单保存 在有其它订阅者时 向订阅者发送value
- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
NSCParameterAssert(subscriber != nil);
return [RACScheduler.subscriptionScheduler schedule:^{
[subscriber sendNext:self.value];
[subscriber sendCompleted];
}];
}RACErrorSignal
用于+ error
+ (RACSignal *)error:(NSError *)error { RACErrorSignal *signal = [[self alloc] init];
signal->_error = error;
#ifdef DEBUG
[signal setNameWithFormat:@"+error: %@", error];
#else
signal.name = @"+error:";
#endif
return signal;
}
- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
return [RACScheduler.subscriptionScheduler schedule:^{
[subscriber sendError:self.error];
}];
}