RxJava 2.x:我应该使用Flowable还是Single / Completable? [英] RxJava 2.x: Should I use Flowable or Single/Completable?

查看:892
本文介绍了RxJava 2.x:我应该使用Flowable还是Single / Completable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Clean Architecture开发Android应用程序,我正在将其迁移到RxJava 2.x.我必须向soap服务发出一些网络请求,所以我在域模块中定义了api接口:

I'm developing an Android app using Clean Architecture and I'm migrating it to RxJava 2.x. I have to make some network requests to a soap service, so I defined the api interface in the domain module:

public interface SiginterApi {
    Observable<User> login(String user, String password);
    ...
    Observable<List<Campaign>> getCampaigns(List<Long> campaignIds);
}

我读过网络请求应该是 Flowable ,因为背压管理,因为它是'冷可观察'。另一方面,我知道请求的结果将是成功(带有响应)或错误,所以我不知道是否应该使用 Flowable 单个甚至 Observable

I've read that a network request should be made with "Flowable", because of the backpressure management since it's a 'cold observable'. On the other hand, I know the result of the request will be success (with the response) or error, so I don't know if I should use Flowable or Single or even Observable.

此外,我有一个数据库这样的访问:

Furthermore, I have a database accesses like this:

public interface UserRepository extends Repository {
    Observable<Void> saveUser(String username, String hashedPassword, boolean logged, User user);
    ...
    Observable<User> findUser(String username, String hashedPassword);
}

我不知道是否应该使用 Completable / Flowable / Obtableable in saveUser 方法和 / Flowable / Observable in findUser 方法。

I don't know if I should use Completable/Flowable/Observable in saveUser method and Single/Flowable/Observable in findUser method.

推荐答案

背压是源<$时的结果c $ c> Observable 比 Subscriber 更快地发出项目消耗它们。这通常是可观察对象的关注,而不是像网络请求那样

Backpressure is what you get when a source Observable is emitting items faster than a Subscriber can consume them. It's most often a concern with hot observables, not cold ones like your network requests.

我认为你应该在 saveUser 中使用 Completable 而不是 Observable< Void> 方法,并对您遵循请求/响应或输入/输出模式的所有位置使用。当您真正想要连续的事件流时,应该使用 Observable

I think you should use Completable instead of Observable<Void> in your saveUser method, and use Single for all places where you follow a request/response or input/output pattern. Observable should be used when you actually want a continuous stream of events.

这篇关于RxJava 2.x:我应该使用Flowable还是Single / Completable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆