You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently I discovered a difference in behavior of replay(1).refCount(). In the RxJava when everyone unsubscribes from refCount() the replay(1) loses its state but in the RxSwift the state is preserved and would be emitted to the next subscriber of refCount(). This difference looks odd, but it is present since RxJava 1.x and up to RxJava 2.x. In the process of investigation I found this very strange (for me) puzzle:
In my world there would be no output in console from this code. In the world of my RxSwift colleagues both values, 42 and 24, would be printed out. But reality is different. In RxJava 2.1.4 42 - would be printed and 24 would not.
Somebody can explain to me this strange behavior?
The text was updated successfully, but these errors were encountered:
Tagakov
changed the title
Stragne behavior of ConnectableObservable
Strange behavior of ConnectableObservable
Sep 28, 2017
The interpretation of what connection and disconnection means is different in these libraries. We largely develop our solutions without much coordination.
In RxJava, the first case disconnects from the subject but the replay remains intact until the next connection, at which point it is reset and starts out empty. Thus, a late subscriber will still get the last item. In the second case, the refCount explicitly resets the replay to its fresh state thus no previous event is preserved.
As far as I know, what you see with RxSwift is similar to the original Rx.NET interpretation: there is an internal ReplaySubject that gets subscribed to the upstream on connect and unsubscribed on disconnection, but otherwise it stays the same instance with the same contents. As long as the main source doesn't complete, you can accumulate data over and over and get all of them replayed.
For a more presistent replay, we have the cache() standard operator and the cacheLast extension operator.
Recently I discovered a difference in behavior of
replay(1).refCount()
. In the RxJava when everyone unsubscribes fromrefCount()
thereplay(1)
loses its state but in the RxSwift the state is preserved and would be emitted to the next subscriber ofrefCount()
. This difference looks odd, but it is present since RxJava 1.x and up to RxJava 2.x. In the process of investigation I found this very strange (for me) puzzle:In my world there would be no output in console from this code. In the world of my RxSwift colleagues both values,
42
and24
, would be printed out. But reality is different. In RxJava 2.1.442
- would be printed and24
would not.Somebody can explain to me this strange behavior?
The text was updated successfully, but these errors were encountered: