RACSignal流程分析

RACSignal流程分析图

下面来进行具体分析:

//1:信号的产生
RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id  _Nonnull subscriber) {
   
    //3:发送信号
    [subscriber sendNext:@"龙晨"];
    
    NSError *error = [[NSError alloc] initWithDomain:NSURLErrorDomain code:1008611 userInfo:@{@"key":@"我的错"}];
    [subscriber sendError:error];
    
    //4:信号销毁
    //RACDisposable 信号回收站
    return [RACDisposable disposableWithBlock:^{
        NSLog(@"信号销毁了");
    }];
}];

//2:订阅信号
[signal subscribeNext:^(id  _Nullable x) {
    NSLog(@"%@",x);
}];

[signal subscribeError:^(NSError * _Nullable error) {
    NSLog(@"%@",error);
}];
  • 1:首先明白代码从上到下依次执行


    流线A
  • 2:流线B:先createSignal


    流线B

    //这里实际上是产生了 RACDynamicSignal 的信号

+ (RACSignal *)createSignal:(RACDisposable * (^)(id subscriber))didSubscribe {
    return [RACDynamicSignal createSignal:didSubscribe];
}
产生RACDynamicSignal里面做的操作.png
第一步产生信号做的事

didSubscriber没有调用执行,代码会继续自上而下进行

流程C
subscriberNext
+ (instancetype)subscriberWithNext:(void (^)(id x))next error:(void (^)(NSError *error))error completed:(void (^)(void))completed {
    RACSubscriber *subscriber = [[self alloc] init];

    //这里subscriber的初始化,保存了NextBlock
    subscriber->_next = [next copy];
    subscriber->_error = [error copy];
    subscriber->_completed = [completed copy];

    return subscriber;
}

//订阅核心代码
- (RACDisposable *)subscribe:(id)subscriber {
    NSCParameterAssert(subscriber != nil);

    //产生了垃圾回收处理中心
    RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
    //这一步也很重要,保存了signal,subscriber,disposeable====> 这样就有了信号的所有元素,方便再次调动信号处理
    subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];

    if (self.didSubscribe != NULL) {
        
        //这里是信号的调度者,后面的文章会给大家详细讲解
        RACDisposable *schedulingDisposable = [RACScheduler.subscriptionScheduler schedule:^{
            //这里就是一个didSubscribe的调用---->这一步之后就会执行之前保存didSubscribe中的代码块
            RACDisposable *innerDisposable = self.didSubscribe(subscriber);
            [disposable addDisposable:innerDisposable];
        }];

        [disposable addDisposable:schedulingDisposable];
    }
    
    return disposable;
}
流线D
订阅信号做的处理
发送信号
发送信号流程

整个流程就这样连成一条线


RACSignal流程分析图

你可能感兴趣的:(RACSignal流程分析)