About using Publish (). RefCount ()

I often find that I want to use Publish().RefCount() to protect my sources.

For example, when translating some incoming JOON service into two IObservable properties:

 var anon = source.Select(TranslateToAnonObject); this.Xs = anon.Select(GetXFromAnonObject); this.Ys = anon.Select(GetYFromAnonObject); 

To avoid doing the translation twice, I would have the desire to put Publish().RefCount() for defining anon.

And the same for both property values ​​so as not to perform Get.. functions separately for each subscriber.

The fact is that he reaches such an extent that I cannot see many situations in which I would not want this. But if it was right, it would certainly be a default in Rx. What am I thinking wrong?

(Thinks: Is it because I almost exclusively work with hot observables?)

+6
source share
1 answer

You very often want to do this. In fact, I wrote an article on this subject. The reason this is not the default is that it is not required all the time (and it is easier to omit than turn it off); There are many cases where it simply increases overhead, and there are more than a few cases where you need to manage publication () with connection control, as the number of subscribers can drop to zero, and re-subscribing will have unintended side effects, especially (as you said ) when working with cold observables.

+5
source

Source: https://habr.com/ru/post/959365/


All Articles