java - android 中 RxJava2 中的 subscribe() 和 subscribeWith() 有什么区别?

标签 java android reactive-programming rx-java2

subscribe()subscribeWith() 在android 中的RxJava2 有什么区别?这两个函数都用于订阅 Observable 上的观察者。这两个功能之间的主要区别是什么? 哪里用subscribe,哪里用subscribeWith。如果可能,请提供代码示例。

最佳答案

Since 1.x Observable.subscribe(Subscriber) returned Subscription, users often added the Subscription to a CompositeSubscription for example:

CompositeSubscription composite = new CompositeSubscription();

composite.add(Observable.range(1, 5).subscribe(new TestSubscriber<Integer>()));

Due to the Reactive-Streams specification, Publisher.subscribe returns void and the pattern by itself no longer works in 2.0. To remedy this, the method E subscribeWith(E subscriber) has been added to each base reactive class which returns its input subscriber/observer as is. With the two examples before, the 2.x code can now look like this since ResourceSubscriber implements Disposable directly:

CompositeDisposable composite2 = new CompositeDisposable();

composite2.add(Flowable.range(1, 5).subscribeWith(subscriber));

来源:What's different in [RxJava] 2.0

关于java - android 中 RxJava2 中的 subscribe() 和 subscribeWith() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951165/

相关文章:

java - 使用 ListModel 作为模型类型从 JList 中删除项目

java - developer.android.com基础训练EXTRA_MESSAGE报错无法解决

安卓和接收短信

java - 我应该使用 "then"还是 "flatMap"来控制流?

java - Android:无法将位图对象绘制到 View 上

java - JPA:未通过 persist() 在数据库中创建实体

java - Samza 0.14.1 无法正确处理 OffsetOutOfRangeException 异常?

android - Cordova/Gradle使用com.android.support:support alpha而不是稳定版

haskell - FRP 是否有一组 "complete"函数/运算符?

javascript - 如何在cycle.js中加载DOM后执行javascript