subscribe()
和subscribeWith()
在android 中的RxJava2 有什么区别?这两个函数都用于订阅 Observable 上的观察者。这两个功能之间的主要区别是什么?
哪里用subscribe,哪里用subscribeWith。如果可能,请提供代码示例。
最佳答案
Since 1.x
Observable.subscribe(Subscriber)
returnedSubscription
, users often added theSubscription
to aCompositeSubscription
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 EsubscribeWith
(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 sinceResourceSubscriber
implementsDisposable
directly:CompositeDisposable composite2 = new CompositeDisposable(); composite2.add(Flowable.range(1, 5).subscribeWith(subscriber));
关于java - android 中 RxJava2 中的 subscribe() 和 subscribeWith() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47951165/