RxJava 多播

Multicasting in RxJava (RxJava中的多播) 原文

Multicasting is a key method for reducing duplicated work in RxJava.
多路是一種解決在RxJava減少重復的有效方法。

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.

當你多播一個事件的時候,你發送了同樣的事件給下流 operators/subscribers.。這是非常有用的,比如當你正在做一個網絡請求操作時。你不想對每個訂閱都做同樣的網絡請求操作--你只是想執行一次網絡請求,然后把請求的結果多播出去。

There are two ways to multicast:
有兩種方式去實現多播:

  • Use a ConnectableObservable (via publish() or replay()1)
    使用一個ConnectableObservable ,通過publish() or replay()操作符實現。
  • 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之前完成工作只會執行一次,接著把結果多播給所有的下流訂閱者。

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.
必須認識到一點:多播只會在使用 ConnectableObservable or Subject那個點發生。因此,對于多播以后的操作每個訂閱者都會去執行。

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()!
從上面的結果來看,map()操作執行了兩次,盡管我們已經使用了publish()來實現多播。也就驗證了只要使用了 ConnectableObservable(通過publish() or replay()操作符實現) or Subjec之后,那么后面的操作每個訂閱者都會去執行。

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()只執行一次,你需要把map()操作放在publish()調用之前:

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.
我們學到了什么? 如果你想要通過multicasting (多播)來減少工作,確定你在正確的地方使用ConnectableObservable(通過publish() or replay() 操作符實現)。

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。一個好處它確實是多播的,但是它只會在發送事件(OnNext, OnComplete等)的地方多播。如果你下游要使用一些昂貴的操作符如(map()),最好在下游的某個地方給這個Subject 加一個publish() 操作符來減少工作。

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()也是一個方法,但是它們也只不過是ConnectableObservable的簡寫。share()可以使用publish().refCount() ,cache()使用replay().autoConnect()來代替。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容