RAC笔记(1)-- RACMulticastConnectio

2020-04-23  本文已影响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方法:

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比较:

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

上一篇下一篇

猜你喜欢

热点阅读