0
votes

I am new to RxJava and Retrofit. Lets say I have Retrofit service which return Observable as

Observable <User> login(String userName, String password);

How can I subscribe to this Observable in multiple places.I want to subscribe it on multiple android activities.

For eg: Lets say I have a Landing page and Login Page Activity. Login page appears after Landing Page. When I make a network request on Login Page the result should be observed on both Login Page as well as Landing Page.

How can I acheive this?

1

1 Answers

4
votes

You can do login(username,pw).cache() - cache() autoconnects to source observable and replays all emitted items to its subscribers. It works well for observables emitting single result, as retrofit does. The other option - replay(), whih returns ConnectableObservable, and subscribes to source only when ConnectableObservable.connect() is called. It has many useful overloads (replay(bufferSize), replay(time, TimeUnit), etc), which allow one to control how many items are cached.