RacDisposable

一、訂閱者都持有disposable

1、 @interface RACPassthroughSubscriber ()
@property (nonatomic, strong, readonly) RACCompoundDisposable *disposable;

2、 @interface RACSubject ()
@property (nonatomic, strong, readonly) RACCompoundDisposable *disposable;

3、@interface RACSubscriber ()
@property (nonatomic, strong, readonly) RACCompoundDisposable *disposable;

4掸鹅、RACChannel
@property (nonatomic, strong, readonly) id<RACSubscriber> otherTerminal;

RacPassthroghSubscriber
RacSubject喜命,
RacSubscriber
Racchannel
didSubscribeWithDisposable

[self.otherTerminal didSubscribeWithDisposable:disposable];
[self.innerSubscriber didSubscribeWithDisposable:self.disposable];
  • (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable {
    if (disposable != self.disposable) {
    [self.disposable addDisposable:disposable];
    }
    }

  • (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];
    }]];
    }

  • (void)didSubscribeWithDisposable:(RACCompoundDisposable *)otherDisposable {
    if (otherDisposable.disposed) return;

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

    @unsafeify(otherDisposable);

    // If this subscription terminates, purge its disposable to avoid unbounded
    // memory growth.
    [otherDisposable addDisposable:[RACDisposable disposableWithBlock:^{
    @strongify(otherDisposable);
    [selfDisposable removeDisposable:otherDisposable];
    }]];
    }

innerSubscriber 具有一個(gè)多態(tài)性的概念 贤重!

RACSubject *a=[RACSubject  subject];
RACCompoundDisposable *disposable =   [RACCompoundDisposable compoundDisposable] ;
RACPassthroughSubscriber *subscriber = [[RACPassthroughSubscriber alloc]  initWithSubscriber:a  signal:a disposable:disposable]  ;
RACCompoundDisposable *disposable2=[RACCompoundDisposable compoundDisposable];
RACPassthroughSubscriber *subscriber2 = [[RACPassthroughSubscriber alloc]  initWithSubscriber:subscriber  signal:a disposable:disposable2]  ;
[a   subscribeNext:^(id  _Nullable x) {
    NSLog(@"222 ->   %@", x);
}];
[subscriber2  sendNext:@"1111"];

二错维、任何訂閱者本質(zhì)上都是RACPassthroughSubscriber。 都持有一個(gè)disposable任内。

其目的是在

RACSubject (@property (nonatomic, strong, readonly) NSMutableArray *subscribers;)
RACPassthroughSubscriber(@property (nonatomic, strong, readonly) id<RACSubscriber> innerSubscriber;)
RACSubscriber////innerSubscriber
再調(diào)用sendXXX 先判斷是否disposable赎瞎。 在sendNext時(shí)牌里,判斷disposable是否已經(jīng)銷毀了,銷毀了則不執(zhí)行block务甥; 在sendError和sendCompleted時(shí)執(zhí)行相關(guān)disposable完成在創(chuàng)建訂閱者時(shí)預(yù)置的銷毀操作牡辽。

1、RACSubject
RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];

[disposable addDisposable:[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];
    }
}]];

return disposable;

2敞临、RACDynamicSignal

RACCompoundDisposable *disposable = [RACCompoundDisposable compoundDisposable];
subscriber = [[RACPassthroughSubscriber alloc] initWithSubscriber:subscriber signal:self disposable:disposable];




if (self.didSubscribe != NULL) {
    RACDisposable *schedulingDisposable = [RACScheduler.subscriptionScheduler schedule:^{
        RACDisposable *innerDisposable = self.didSubscribe(subscriber);
        [disposable addDisposable:innerDisposable];
    }];

    [disposable addDisposable:schedulingDisposable];
}

return disposable;

三态辛、RacDsiposable類

RACDisposable

RACCompoundDisposable

RACKVOTrampoline

RACScopedDisposable

RACSerialDisposable

RACSerialDisposable 在Rac源代碼出現(xiàn):
1、RACMulticastConnection

if (shouldConnect) {
    self.serialDisposable.disposable = [self.sourceSignal subscribe:_signal];
}

2挺尿、RacSignal.m

Bind方法中
selfDisposable.disposable = bindingDisposable;

3奏黑、RACSignal+Operations.m

1、throttle
nextDisposable.disposable = [delayScheduler afterDelay:interval schedule:^{
flushNext(YES);
}];

2编矾、catch

        catchDisposable.disposable = [signal subscribe:subscriber];

3熟史、bufferWithTime
if (values.count == 0) {
timerDisposable.disposable = [scheduler afterDelay:interval schedule:flushValues];
}

4、flatten
serialDisposable.disposable = [signal subscribeNext:^(id x) {
[subscriber sendNext:x];
} error:^(NSError *error) {
[subscriber sendError:error];
} completed:^{

5窄俏、takeUntilReplacement
if (!selfDisposable.disposed) {
selfDisposable.disposable = [[self
concat:[RACSignal never]]
subscribe:subscriber];
}

6蹂匹、sample

    samplerDisposable.disposable = [sampler subscribeNext:^(id _) {
        BOOL shouldSend = NO;
        id value;
        [lock lock];
        shouldSend = hasValue;
        value = lastValue;
        [lock unlock];

        if (shouldSend) {
            [subscriber sendNext:value];
        }
    } error:^(NSError *error) {
        [sourceDisposable dispose];
        [subscriber sendError:error];
    } completed:^{
        [sourceDisposable dispose];
        [subscriber sendCompleted];
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市凹蜈,隨后出現(xiàn)的幾起案子限寞,更是在濱河造成了極大的恐慌,老刑警劉巖仰坦,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件履植,死亡現(xiàn)場離奇詭異,居然都是意外死亡缎岗,警方通過查閱死者的電腦和手機(jī)静尼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進(jìn)店門白粉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來传泊,“玉大人,你說我怎么就攤上這事鸭巴【煜福” “怎么了?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵鹃祖,是天一觀的道長溪椎。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么校读? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任沼侣,我火速辦了婚禮,結(jié)果婚禮上歉秫,老公的妹妹穿的比我還像新娘蛾洛。我一直安慰自己,他們只是感情好雁芙,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布轧膘。 她就那樣靜靜地躺著,像睡著了一般兔甘。 火紅的嫁衣襯著肌膚如雪谎碍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天洞焙,我揣著相機(jī)與錄音蟆淀,去河邊找鬼。 笑死澡匪,一個(gè)胖子當(dāng)著我的面吹牛扳碍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播仙蛉,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼笋敞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了荠瘪?” 一聲冷哼從身側(cè)響起夯巷,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哀墓,沒想到半個(gè)月后趁餐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡篮绰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年后雷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吠各。...
    茶點(diǎn)故事閱讀 40,928評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡臀突,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出贾漏,到底是詐尸還是另有隱情候学,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布纵散,位于F島的核電站梳码,受9級特大地震影響隐圾,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜掰茶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一暇藏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧濒蒋,春花似錦叨咖、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至焰坪,卻和暖如春趣倾,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背某饰。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工儒恋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人黔漂。 一個(gè)月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓诫尽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親炬守。 傳聞我的和親對象是個(gè)殘疾皇子牧嫉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,937評論 2 361

推薦閱讀更多精彩內(nèi)容