简体   繁体   English

单个网络的多个订阅请求翻新和RXJava?

[英]Multiple Subscription to single network request Retrofit and RXJava?

I am new to RxJava and Retrofit. 我是RxJava和Retrofit的新手。 Lets say I have Retrofit service which return Observable as 可以说我有Retrofit服务,其返回Observable为

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. 我如何在多个地方订阅此Observable。我想在多个android活动上订阅它。

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? 我该如何实现?

You can do login(username,pw).cache() - cache() autoconnects to source observable and replays all emitted items to its subscribers. 您可以执行login(username,pw).cache() - cache()到可观察到的源,并将所有发出的项目重放给其订阅者。 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. 另一个选项-replay replay() ,它返回ConnectableObservable ,并且仅在调用ConnectableObservable.connect()时订阅源。 It has many useful overloads ( replay(bufferSize) , replay(time, TimeUnit) , etc), which allow one to control how many items are cached. 它有许多有用的重载( replay(bufferSize)replay(time, TimeUnit)等),它使您可以控制缓存多少个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM