接著上一篇《RAC學(xué)習(xí)(一)》
一、RACReplaySubject
使用RACReplaySubject
進(jìn)行信號(hào)的發(fā)送訂閱
//1窜觉、創(chuàng)建信號(hào)
RACReplaySubject * subject = [RACReplaySubject subject];
//2、訂閱信號(hào)
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"XX == %@",x);
}];
//3禀挫、發(fā)送信號(hào)
[subject sendNext:@"111"];
這樣一看與RACSubject
沒(méi)有任何區(qū)別?描孟?這樣是沒(méi)有任何區(qū)別的砰左!
RACSubject
如果這樣呢?將 發(fā)送信號(hào) 與 訂閱信號(hào) 交換下
//1廉羔、創(chuàng)建信號(hào)
RACSubject * subject = [RACSubject subject];
//3僻造、發(fā)送信號(hào)
[subject sendNext:@"111"];
//2孩饼、訂閱信號(hào)
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"XX == %@",x);
}];
結(jié)果:不會(huì)收到訂閱信息
換成RACReplaySubject
//1竹挡、創(chuàng)建信號(hào)
RACReplaySubject * subject = [RACReplaySubject subject];
//3、發(fā)送信號(hào)
[subject sendNext:@"111"];
//2汽畴、訂閱信號(hào)
[subject subscribeNext:^(id _Nullable x) {
NSLog(@"XX == %@",x);
}];
結(jié)果:收到了訂閱信息K市颉!坎怪!
二、源碼分析
1嘁酿、查看sendNext
2男应、先進(jìn)入查看subscribeNext
的內(nèi)部代碼
3沐飘、更深入查看
這里會(huì)發(fā)現(xiàn)一個(gè)情況,就是將之前保存數(shù)據(jù)的數(shù)組
valueReceived
進(jìn)行遍歷查看是否由信號(hào)源借卧,如果有就再次發(fā)送筛峭,如果沒(méi)有就pass