Multicasting in RxJava (RxJava中的多播) 原文
Multicasting is a key method for reducing duplicated work in RxJava.
多路是一種解決在RxJava減少重復(fù)的有效方法闪萄。
When you multicast an event, you send the same event to all downstream operators/subscribers. This is useful when you're doing an expensive operation like a network request. You don't want to repeatedly execute identical network requests for each subscriber - you just want to execute one then multicast the results.
當(dāng)你多播一個(gè)事件的時(shí)候闷营,你發(fā)送了同樣的事件給下流 operators/subscribers.皮仁。這是非常有用的剃根,比如當(dāng)你正在做一個(gè)網(wǎng)絡(luò)請(qǐng)求操作時(shí)。你不想對(duì)每個(gè)訂閱都做同樣的網(wǎng)絡(luò)請(qǐng)求操作--你只是想執(zhí)行一次網(wǎng)絡(luò)請(qǐng)求鹰祸,然后把請(qǐng)求的結(jié)果多播出去甫窟。
There are two ways to multicast:
有兩種方式去實(shí)現(xiàn)多播:
- Use a ConnectableObservable (via publish() or replay()1)
使用一個(gè)ConnectableObservable ,通過(guò)publish() or replay()操作符實(shí)現(xiàn)蛙婴。 - Use a Subject
Any work done before the ConnectableObservable or Subject will only happen once, then that work will be multicast to all downstream Subscribers.
任何在ConnectableObservable or Subject之前完成工作只會(huì)執(zhí)行一次粗井,接著把結(jié)果多播給所有的下流訂閱者。
There is a subtle point here you must recognize: Streams only multicast at the point of the ConnectableObservable or Subject. As a result, any work done after the multicast is duplicated per Subscriber.
必須認(rèn)識(shí)到一點(diǎn):多播只會(huì)在使用 ConnectableObservable or Subject那個(gè)點(diǎn)發(fā)生街图。因此浇衬,對(duì)于多播以后的操作每個(gè)訂閱者都會(huì)去執(zhí)行。
Let's look at an example of how this can come into play:
Observable<String> observable = Observable.just("Event")
.publish()
.autoConnect(2)
.map(s -> {
System.out.println("Expensive operation for " + s);
return s;
});
observable.subscribe(s -> System.out.println("Sub1 got: " + s));
observable.subscribe(s -> System.out.println("Sub2 got: " + s));
// Output:
// Expensive operation for Event
// Sub1 got: Event
// Expensive operation for Event
// Sub2 got: Event
Here we have a ConnectableObservable, an expensive map() operation and two Subscribers. The surprising result is that the expensive map() operation is executed twice, even though we tried to prevent that with publish()!
從上面的結(jié)果來(lái)看餐济,map()操作執(zhí)行了兩次耘擂,盡管我們已經(jīng)使用了publish()來(lái)實(shí)現(xiàn)多播。也就驗(yàn)證了只要使用了 ConnectableObservable(通過(guò)publish() or replay()操作符實(shí)現(xiàn)) or Subjec之后絮姆,那么后面的操作每個(gè)訂閱者都會(huì)去執(zhí)行醉冤。
This chart makes the situation clearer:
Chart showing why publish didn't work
If you actually wanted the map() to happen once, you would need to put it before the publish() call:
如果你想要map()只執(zhí)行一次,你需要把map()操作放在publish()調(diào)用之前:
Observable<String> observable = Observable.just("Event")
.map(s -> {
System.out.println("Expensive operation for " + s);
return s;
})
.publish()
.autoConnect(2);
observable.subscribe(s -> System.out.println("Sub1 got: " + s));
observable.subscribe(s -> System.out.println("Sub2 got: " + s));
// Output:
// Expensive operation for Event
// Sub1 received: Event
// Sub2 received: Event
Here's an updated chart:
Chart with correct publish usage
What's the lesson here? If you're relying on multicasting to reduce work, make sure you multicast at the right point.
我們學(xué)到了什么篙悯? 如果你想要通過(guò)multicasting (多播)來(lái)減少工作蚁阳,確定你在正確的地方使用ConnectableObservable(通過(guò)publish() or replay() 操作符實(shí)現(xiàn))。
For better or worse, a lot of people are using Subjects. One advantage is that they are multicast but you have to remember that they are only multicast at the point they emit. If you have a bunch of expensive operators applied downstream of the Subject then you should consider adding another publish() somewhere downstream.
不論好壞鸽照,好多人使用Subjects螺捐。一個(gè)好處它確實(shí)是多播的,但是它只會(huì)在發(fā)送事件(OnNext, OnComplete等)的地方多播矮燎。如果你下游要使用一些昂貴的操作符如(map())定血,最好在下游的某個(gè)地方給這個(gè)Subject 加一個(gè)publish() 操作符來(lái)減少工作。
share() and cache() are also options, but they are basically just shortcuts around ConnectableObservable. share() is just publish().refCount() and cache() can be recreated by using replay().autoConnect().
share()和cache()也是一個(gè)方法诞外,但是它們也只不過(guò)是ConnectableObservable的簡(jiǎn)寫(xiě)澜沟。share()可以使用publish().refCount() ,cache()使用replay().autoConnect()來(lái)代替峡谊。