美文网首页
RAC笔记(1)-- RACMulticastConnectio

RAC笔记(1)-- RACMulticastConnectio

作者: 喵喵粉 | 来源:发表于2020-04-23 18:14 被阅读0次

    RACMulticastConnection

    - (void)mutlticastConnect {
        RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {//subscriber:RACSubject->RACPassthroughSubscriber
            NSLog(@"发送请求");
            [subscriber sendNext:@"发送数据"];
            return nil;
        }];
        RACMulticastConnection *connect = [signal publish];
        [connect.signal subscribeNext:^(id  _Nullable x) {//connect->_signal: <RACSubject
            NSLog(@"11:%@", x);
        }];
        [connect.signal subscribeNext:^(id  _Nullable x) {
            NSLog(@"22:%@", x);
        }];
        [connect.signal subscribeNext:^(id  _Nullable x) {
            NSLog(@"33:%@", x);
        }];
        [connect connect];//执行signal的block:connect.sourceSignal.didSubscribe(connect.subject)
        [connect.signal subscribeNext:^(id  _Nullable x) {
            NSLog(@"44:%@", x);
        }];
        [connect connect];
        [connect.signal subscribeNext:^(id  _Nullable x) {
            NSLog(@"55:%@", x);
        }];
    }
    
    ///打印
    2020-04-23 18:22:15.932457+0800 RacDemo[1626:631230] 发送请求
    2020-04-23 18:22:15.932587+0800 RacDemo[1626:631230] 11:发送数据
    2020-04-23 18:22:15.932632+0800 RacDemo[1626:631230] 22:发送数据
    2020-04-23 18:22:15.932670+0800 RacDemo[1626:631230] 33:发送数据
    
    1. 创建原始信号,保存blockdidSubscribe
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
    }];
    
    image.png
    1. 创建RACMulticastConnectionconnect
      RACSignal对象signal调用publish得到
    RACMulticastConnection *connect = [signal publish];
    

    publish方法:

    • 将原始信号signal赋值给_sourceSignal_sourceSignal = source;
    • 创建RACSubject对象赋值给_signal_signal = subject;
    publish方法
    - (instancetype)initWithSourceSignal:(RACSignal *)source subject:(RACSubject *)subject {
        NSCParameterAssert(source != nil);
        NSCParameterAssert(subject != nil);
    
        self = [super init];
    
        _sourceSignal = source;
        _serialDisposable = [[RACSerialDisposable alloc] init];
        _signal = subject;
        
        return self;
    }
    
    1. connect.signalconnect. subject调用订阅方法subscribeNext
    [connect.signal subscribeNext:^(id  _Nullable x) {//connect->_signal: <RACSubject
         NSLog(@"11:%@", x);
    }];
    

    将订阅者RACSubscriber封装成RACPassthroughSubscriber类型,保存到订阅者subscribers数组

    subscribeNext RACSubject.subscribe
    1. [connect connect]

    原始信号signal订阅subject

    [connect connect]

    subject包装成RACPassthroughSubscriber,做为参数发给原始信号signalblockdidSubscribe

    RACDynamicSignal.subscribe

    执行blockdidSubscribe(RACPassthroughSubscriber)

    didSubscribe(RACPassthroughSubscriber)
    1. RACPassthroughSubscribersendNext
    [subscriber sendNext:@"发送数据"];
    
    RACPassthroughSubscriber.sendNext

    RACPassthroughSubscriber.innerSubscriberRACSubject类型,来到RACSubject.sendNext,遍历保存的订阅者数组subscribers,发送数据

    RACSubject.sendNext
    1. connect之后的订阅不会执行
    打印 connect

    OSAtomicCompareAndSwap32Barrier_hasConnected1比较:

    • _hasConnected != 1:返回YES_hasConnected = 1
    • _hasConnected == 1:返回NO

    connect之后的订阅不会执行blockdidSubscribe,所以后面的44、55不打印

    相关文章

      网友评论

          本文标题:RAC笔记(1)-- RACMulticastConnectio

          本文链接:https://www.haomeiwen.com/subject/qvgaihtx.html