RACSignal创建流程
//1、创建信号
RACSignal * signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber> _Nonnull subscriber) {
//3、发送信号
[subscriber sendNext:@"LGCooci"];
return nil;
}];
//2、订阅信号
[signal subscribeNext:^(id _Nullable x) {
NSLog(@"订阅到了%@",x);
}];
- 创建信号 -
createSignal()
- 订阅信号 -
subscribeNext()
- 发送信号 -
sendNext()
sendCompleted()
sendError()
核心逻辑源码分析
创建信号
+ (RACSignal *)createSignal:(RACDisposable * (^)(id<RACSubscriber> subscriber))didSubscribe {
return [RACDynamicSignal createSignal:didSubscribe];
}
+ (RACSignal *)createSignal:(RACDisposable * (^)(id<RACSubscriber> subscriber))didSubscribe {
RACDynamicSignal *signal = [[self alloc] init];
signal->_didSubscribe = [didSubscribe copy];
return [signal setNameWithFormat:@"+createSignal:"];
}
createSignal
带入一个block:didSubscribe
RACDynamicSignal
创建信号,并保存block:_didSubscribe
订阅信号
- (RACDisposable *)subscribeNext:(void (^)(id x))nextBlock {
NSCParameterAssert(nextBlock != NULL);
RACSubscriber *o = [RACSubscriber subscriberWithNext:nextBlock error:NULL completed:NULL];
return [self subscribe:o];
}
+ (instancetype)subscriberWithNext:(void (^)(id x))next error:(void (^)(NSError *error))error completed:(void (^)(void))completed {
RACSubscriber *subscriber = [[self alloc] init];
subscriber->_next = [next copy];
subscriber->_error = [error copy];
subscriber->_completed = [completed copy];
return subscriber;
}
RACSubscriber
创建订阅者o
, 传入block:nextBlock
RACSubscriber
内部保存blcok:nextBlock
的_next
,_error
,_completed
- 调用
[self subscribe:o]
,此时的self
即为:RACDynamicSignal
对象
进入RACDynamicSignal
的subscribe
内部:
- (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
NSCParameterAssert(subscriber != nil);
RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];
if (self.didSubscribe != NULL) {
RACDisposable *schedulingDisposable = [RACScheduler.subscriptionScheduler schedule:^{
RACDisposable *innerDisposable = self.didSubscribe(subscriber);
[disposable addDisposable:innerDisposable];
}];
[disposable addDisposable:schedulingDisposable];
}
return disposable;
}
RACPassthroughSubscriber
(接受了RACSubscriber
协议)传入subscriber
重新初始化了新的subscriber
- 调用
self.didSubscribe(subscriber)
, 这里的didSubscribe
即为创建信号时候保存的block, 这里就直接就把subscriber
回调了回去
发送信号
//3、发布信号
[subscriber sendNext:@"LGCooci"];
[subscriber sendCompleted];
- (void)sendNext:(id)value {
@synchronized (self) {
void (^nextBlock)(id) = [self.next copy];
if (nextBlock == nil) return;
nextBlock(value);
}
}
- (void)sendError:(NSError *)e {
@synchronized (self) {
void (^errorBlock)(NSError *) = [self.error copy];
[self.disposable dispose];
if (errorBlock == nil) return;
errorBlock(e);
}
}
- (void)sendCompleted {
@synchronized (self) {
void (^completedBlock)(void) = [self.completed copy];
[self.disposable dispose];
if (completedBlock == nil) return;
completedBlock();
}
}
- 这里的
subscriber
即为订阅信号调用didSubscribe
回调传出来的RACPassthroughSubscriber
对象, 当调用sendNext
,sendError
,sendCompleted
的时候,就会通过保存的.next, .error, .completed
block进行回调,最后就完成了整个流程