一张图理解RACSignal的Subscription过程
通過下面一張圖理解RACSignal的調用過程:
創建signale
RACSignal通過子類[RACDynamicSignal createSignal:]方法獲得Signal,并將disSubscribe這個block保存在Signal中。
+ (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:"]; }創建subscriber
signal通過調用subscribeNext方法生成subscriber,并將next、error、completed block保存在subscriber中
- (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; }進行subscribe
第二步創建subscriber之后調用signal的subscribe方法,并將創建的subscriber作為參數。
這一步會生成RACCompoundDisposable和RACPassthroughSubscriber對象。
- RACCompoundDisposable:RACDisposable的子類,可以加入多個RACDisposable對象。當RACCompoundDisposable對象被dispose的時候,會dispose容器內的所有RACDisposable對象。
- RACPassthroughSubscriber:分別保存對RACSignal,RACSubscriber,RACCompoundDisposable的引用。通過RACPassthroughSubscriber對象來轉發給真正的Subscriber。
執行disSubscribe block
RACSignal通過RACScheduler.subscriptionScheduler來執行閉包,disSubscribe真正被調用的的位置就是上一步的RACDisposable *innerDisposable = self.didSubscribe(subscriber);
- (RACDisposable *)schedule:(void (^)(void))block {NSCParameterAssert(block != NULL);if (RACScheduler.currentScheduler == nil) return [self.backgroundScheduler schedule:block];block();return nil; }調用sendNext sendError sendCompleted
進入didSubscribe閉包后,調用sendNext:、sendError:、sendCompleted。由于第三步中將subscriber替換為RACPassthroughSubscriber對象,真正的subscriber被存儲在RACPassthroughSubscriber對象中,即innerSubscriber,所以這一步的各種send方法其實是一個轉發過程。
- (void)sendNext:(id)value {if (self.disposable.disposed) return;if (RACSIGNAL_NEXT_ENABLED()) {RACSIGNAL_NEXT(cleanedSignalDescription(self.signal), cleanedDTraceString(self.innerSubscriber.description), cleanedDTraceString([value description]));}[self.innerSubscriber sendNext:value]; }- (void)sendError:(NSError *)error {if (self.disposable.disposed) return;if (RACSIGNAL_ERROR_ENABLED()) {RACSIGNAL_ERROR(cleanedSignalDescription(self.signal), cleanedDTraceString(self.innerSubscriber.description), cleanedDTraceString(error.description));}[self.innerSubscriber sendError:error]; }- (void)sendCompleted {if (self.disposable.disposed) return;if (RACSIGNAL_COMPLETED_ENABLED()) {RACSIGNAL_COMPLETED(cleanedSignalDescription(self.signal), cleanedDTraceString(self.innerSubscriber.description));}[self.innerSubscriber sendCompleted]; }執行next error completed閉包
通過調用innerSubscriber的sendNext:、sendError、和sendCompleted方法執行真正的subscriber中的next error completed閉包
- (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();} }過程回顧
去掉中間的繁雜細節,大致過程如下:
1.通過createSignal生成信號
2.通過subscribeNext確定信號內容到來時的處理方式
3.didSubscribe block塊中異步處理完畢之后,進行sendNext、sendError和sendCompleted自動處理
轉載于:https://www.cnblogs.com/wanyakun/p/6472752.html
總結
以上是生活随笔為你收集整理的一张图理解RACSignal的Subscription过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LVS负载均衡-NAT模式
- 下一篇: 粗粮馆宣传标语文案28句