ReactiveCocoa学习笔记七-RACSubscriber源码分析

Subscriber

到了最后一个部分了--订阅者. 其实在Signal篇我们已经接触过了一些Subscriber, 毕竟Subject也是一个Subscriber.

而与RACSignal和RACDisposable不同的是, 与Subscriber相关的其它类不是用的继承的形式, 而是组合(也就是其它类持有了一个RACSubscriber实例, 例如RACPassthroughSubscriber).

RACSubscriber

RACSubcriber是一般订阅者的类, 整体而言代码也是比较简洁的, 首先要说明的是RACSubscriber有一个protocol的声明, 也有一个类的声明, 对于一个协议来说, 它Required了以下几个方法:

- (void)sendNext:(id)value;

- (void)sendError:(NSError *)error;

- (void)sendCompleted;

- (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable;

前面3个基本都见过了, 最后一个在Subject也见过一次. RACSubscriber类有个类方法返回实例, 但是头文件标注为私有的, 也就是说一般我们使用的时候不要关注它:

+ (instancetype)subscriberWithNext:(void (^)(id x))next error:(void (^)(NSError *error))error completed:(void (^)(void))completed;

再看看私有变量:

@interface RACSubscriber ()

@property (nonatomic, copy) void (^next)(id value);
@property (nonatomic, copy) void (^error)(NSError *error);
@property (nonatomic, copy) void (^completed)(void);
// 因为协议中有了addDisposable, 所以理所当然要有个RACCompoundDisposable实例
@property (nonatomic, strong, readonly) RACCompoundDisposable *disposable;

@end

再看看几个重要方法:

- (id)init {
    self = [super init];
    if (self == nil) return nil;

    @unsafeify(self);

    // 首先要添加一个释放自己资源的disposable
    RACDisposable *selfDisposable = [RACDisposable disposableWithBlock:^{
        @strongify(self);

        @synchronized (self) {
            self.next = nil;
            self.error = nil;
            self.completed = nil;
        }
    }];

    _disposable = [RACCompoundDisposable compoundDisposable];
    [_disposable addDisposable:selfDisposable];

    return self;
}

- (void)dealloc {
    [self.disposable dispose];
}

值得一提的是, 虽然与Subscriber与Subject的作者都是同一人, 但是2者创建的时间却是一个1月一个9月, 所以在写的时候还是会有所区别, 特别是@unsafeify和@weakify, 之前看到一篇博文应该是王巍写的, 专门讲RAC里面宏的精要, 还是很值得一看的.

再看看3个发送事件的方法:

- (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();
    }
}

可以看到前期作者在写的时候非常谨小慎微, 各种同步加持, 还用临时变量来保证线程安全, 到了Subject的时候就会更加"随意"一些.

同样的, 最后这个didSubscribeWithDisposable也是如此, 这里以这个方法来看看Subject里面和Subscriber里面的实现差异

// RACSubscriber.m
- (void)didSubscribeWithDisposable:(RACCompoundDisposable *)otherDisposable {
    if (otherDisposable.disposed) return;

    RACCompoundDisposable *selfDisposable = self.disposable;
    [selfDisposable addDisposable:otherDisposable];

    @unsafeify(otherDisposable);

    [otherDisposable addDisposable:[RACDisposable disposableWithBlock:^{
        @strongify(otherDisposable);
        [selfDisposable removeDisposable:otherDisposable];
    }]];
}
// RACSubject.m
- (void)didSubscribeWithDisposable:(RACCompoundDisposable *)d {
    if (d.disposed) return;
    [self.disposable addDisposable:d];

    @weakify(self, d);
    [d addDisposable:[RACDisposable disposableWithBlock:^{
        @strongify(self, d);
        [self.disposable removeDisposable:d];
    }]];
}

发现去掉临时变量之后, 二者基本上是一致的.

RACPassthroughSubscriber

在我们订阅的时候, 实际创建的实例是RACPassthroughSubscriber, 但是一般我们不直接创建它, 都是RAC本身的实现.

先看看头文件:

@interface RACPassthroughSubscriber : NSObject 

- (instancetype)initWithSubscriber:(id)subscriber signal:(RACSignal *)signal disposable:(RACCompoundDisposable *)disposable;

实现了RACSubscriber协议, 并且signal, subcriber, disposable一应俱全, 看起来这个类会维系好三者之间的关系.

看实现文件时, 发现里面又来了一些 DTrace 的东西, 这个东西在 objc.io 里面有过介绍, Xcode 的Instruments就是基于此实现, 有兴趣的可以去看看. 从这个侧面也应该看的出来, RAC 团队对框架的性能是有过比较深度的评测的, 因此还是值得信赖的.

同时需要提一下的是, DTrace 的属性没有标注为weak 而是unsafe_unretained, 众所周知, weak 的属性会在指向的对象被释放后自动置为nil, 因此要实现这一点, 肯定就会在目标对象上有存储 weak 引用的列表, 据测为了保证 weak 引用, 中间产生的开销还是不小的, 所以 RAC 会只在 DTrace 中用的属性用unsafe_unretained, 以免造成不必要的开销. 当然, 使用了unsafe_unretained的属性, 就不能轻易去 get 和 set 否则很容易造成野指针 crash.

实例初始化代码:

- (instancetype)initWithSubscriber:(id)subscriber signal:(RACSignal *)signal disposable:(RACCompoundDisposable *)disposable {
    NSCParameterAssert(subscriber != nil);

    self = [super init];
    if (self == nil) return nil;

    _innerSubscriber = subscriber;
    _signal = signal;
    _disposable = disposable;

    // 添加传入的 disposable, 在调用 dispose 的时候一并清除掉
    [self.innerSubscriber didSubscribeWithDisposable:self.disposable];
    return self;
}

RACSubscriber协议的其它方法就基本上没有太多可讲的了, 与上面的基本上是大同小异. 那么这里会引出一个问题, 为什么不直接用 RACSubscriber, 而要再引入一个 passThrough 版本呢?

根据RACPassthroughSubscriber头文件中对这个类的描述:

A private subscriber that passes through all events to another subscriber
while not disposed.

大意是说: 在未被清除的情况下, 把所有事件都传递到另一个订阅者的私有订阅者.

前提很好理解, 清除之后自然没有必要再传递, 另一个订阅者自然也就是传入进来的这个订阅者, 也就是实际上需要发送时间的 RACSubscriber. 然而这段话并没有解释出来为什么需要这么样个私有订阅者呢, 从代码角度上来看也就是加了 DTrace 的功能而已, 并没有什么特殊的管理三者关系的代码存在.

目前除了 DTrace 的探针之外, 我个人找不到很好的解释, 发送邮件给作者想要问这个问题也暂时没有得到回复, 如果有回复我会更新上来. 除此之外, 我还特地把RACDynamicSignal里面的subscribe中把普通 RACSubscriber 变为RACPassThroughSubscriber 的代码给注释掉:

// subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];

发现对我的 demo 应用并没有任何干扰, 因此也算一个佐证证明RACPassThroughSubscriber的作用的猜想了.

你可能感兴趣的:(ReactiveCocoa学习笔记七-RACSubscriber源码分析)