美文网首页
RAC中的RACSubject

RAC中的RACSubject

作者: 一只二进制编码的狗 | 来源:发表于2016-01-28 11:41 被阅读2805次

    rac中热信号的源头,继承于RACSignal
    用法比较简单,如下:

        RACSubject *subject = [RACSubject subject];
        [subject subscribeNext:^(id x) {
            NSLog(@"订阅者1%@", x);
        }];
        
        [subject sendNext:@"subject1"];
        
        [subject subscribeNext:^(id x) {
            NSLog(@"订阅者2%@", x);
        }];
        
        [subject sendNext:@"subject2"];
    2016-01-28 11:32:40.028 demo[7131:42883] 订阅者1subject1
    2016-01-28 11:32:40.029 demo[7131:42883] 订阅者1subject2
    2016-01-28 11:32:40.029 demo[7131:42883] 订阅者2subject2
    

    看log可以看出racsubject和racsignal有本质的区别,不关心历史值,错过就错过了,具体原因看源码:

    //初始化没什么好说的
    + (instancetype)subject {
        return [[self alloc] init];
    }
    
    - (id)init {
        self = [super init];
        if (self == nil) return nil;
    
        _disposable = [RACCompoundDisposable compoundDisposable];
        _subscribers = [[NSMutableArray alloc] initWithCapacity:1];
        
        return self;
    }
    
    - (void)dealloc {
        [self.disposable dispose];
    }
    
    #pragma mark Subscription
    
    - (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
        NSCParameterAssert(subscriber != nil);
    
        RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
        subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];
    //核心代码就是把订阅者加入到subscribers这个数组中
        NSMutableArray *subscribers = self.subscribers;
        @synchronized (subscribers) {
            [subscribers addObject:subscriber];
        }
        
        return [RACDisposable disposableWithBlock:^{
            @synchronized (subscribers) {
                // Since newer subscribers are generally shorter-lived, search
                // starting from the end of the list.
                NSUInteger index = [subscribers indexOfObjectWithOptions:NSEnumerationReverse passingTest:^ BOOL (id<RACSubscriber> obj, NSUInteger index, BOOL *stop) {
                    return obj == subscriber;
                }];
    
                if (index != NSNotFound) [subscribers removeObjectAtIndex:index];
            }
        }];
    }
    
    - (void)enumerateSubscribersUsingBlock:(void (^)(id<RACSubscriber> subscriber))block {
        NSArray *subscribers;
        @synchronized (self.subscribers) {
            subscribers = [self.subscribers copy];
        }
    
        for (id<RACSubscriber> subscriber in subscribers) {
            block(subscriber);
        }
    }
    
    #pragma mark RACSubscriber
    
    - (void)sendNext:(id)value {
        [self enumerateSubscribersUsingBlock:^(id<RACSubscriber> subscriber) {
    //当sendnext的时候,就会遍历这个subscribers数组,然后把值发送出去,所以对历史值不关心。
            [subscriber sendNext:value];
        }];
    }
    
    - (void)sendError:(NSError *)error {
        [self.disposable dispose];
        
        [self enumerateSubscribersUsingBlock:^(id<RACSubscriber> subscriber) {
            [subscriber sendError:error];
        }];
    }
    
    - (void)sendCompleted {
        [self.disposable dispose];
        
        [self enumerateSubscribersUsingBlock:^(id<RACSubscriber> subscriber) {
            [subscriber sendCompleted];
        }];
    }
    
    - (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];
        }]];
    }
    

    相关文章

      网友评论

          本文标题: RAC中的RACSubject

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