rx-java

HTTP FAILED: java.net.SocketException: Socket closed; doesn't fire up exception handling methods

假装没事ソ 提交于 2020-01-02 04:12:50
问题 I have an RxJava chain request that should release some locks onError(), or onComplete(), so, basically, what my problem is: when I set read, connect and write timeouts to my OkHttpClient, I don't get the desired behavior. I'm using Retrofit2 and OkHttp3.6.0 Here's my simplified client: OkHttpClient.Builder builder = new OkHttpClient.Builder() .readTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) OkHttpClient okHttpClient = builder.build(

PublishSubject with kotlin coroutines (Flow)

被刻印的时光 ゝ 提交于 2020-01-02 02:37:05
问题 I'm trying to refactor a piece of code from RX to coroutines but despite all my efforts I think I'm getting lost. So I had PublishSubject created and i was sending messages to it and also I was listening for results. It worked flawlessly, but now I'm not sure how to do the same thing with coroutines (flows or channels). private val subject = PublishProcessor.create<Boolean>>() ... fun someMethod(b: Boolean) { subject.onNext(b) } fun observe() { subject.debounce(500, TimeUnit.MILLISECONDS)

What is RxJava equivalent of orElse

拟墨画扇 提交于 2020-01-02 01:20:06
问题 There is one common operation in stream/functional land in other languages, that's orElse(). It serves like an if, where when the current chain didn't get any result, it changes to the alternate one. In a language with Maybe types it'd basically continue the chain for a Some type or change to the orElse on a None type. Ideal case: Observable.just(false) .filter(value -> { return value; }) .map(value -> { return 1; }) .orElse(Observable.just(0)); Observable.<Boolean>error(new

Downloading files using OkHttp, Okio and RxJava

牧云@^-^@ 提交于 2020-01-01 19:48:21
问题 I'm trying to download files using OkHttp and writing to disk with Okio. Also I've created an rx observable for this process. It is working, however it is noticeably slower than what I had previously used (Koush's Ion library). Here's how I create the observable: public Observable<FilesWrapper> download(List<Thing> things) { return Observable.from(things) .map(thing -> { File file = new File(getExternalCacheDir() + File.separator + thing.getName()); if (!file.exists()) { Request request = new

How can I avoid repeating common observable configuration?

随声附和 提交于 2020-01-01 18:19:31
问题 I'm writing an API client in Android using Retrofit and this sort of code gets repeated a lot: myObservableFromRetrofit .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .doOnError(... print stack trace ...) I'm wondering if there is a technique to avoid repeating this stuff. I surrounding calls to retrofit functions with: public Observable<?> commonObservable(Observable<?> observable) { return observable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers

How to handle dispose in RxJava without InterruptedException

。_饼干妹妹 提交于 2020-01-01 17:07:09
问题 In the below code snipped when dispose() is called, then the emitter thread is interrupted ( InterruptedException is thrown out of sleep method). Observable<Integer> obs = Observable.create(emitter -> { for (int i = 0; i < 10; i++) { if (emitter.isDisposed()) { System.out.println("> exiting."); emitter.onComplete(); return; } emitter.onNext(i); System.out.println("> calculation = " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } emitter.onComplete()

RxJava - observe data that might always change

陌路散爱 提交于 2020-01-01 09:12:34
问题 I'm trying to jump on the reactive bandwagon, but after reading and going through lots of examples, I still haven't found what I'm looking for. I have a model object which might get change during any time in the lifecycle of the application. The changes might come from a specific request to update it (from servers, db, etc) or it might get updated due an event which fires in the app. My question is how do I create an Observable of such an object and how do I keep updating the subscribers when

How to return value with RxJava?

匆匆过客 提交于 2020-01-01 08:43:35
问题 Let's consider this situation. We have some class which has one method which returns some value: public class Foo() { Observer<File> fileObserver; Observable<File> fileObservable; Subscription subscription; public File getMeThatThing(String id) { // implement logic in Observable<File> and return value which was // emitted in onNext(File) } } How to return that value which was received in onNext ? What would be the correct approach? Thank you. 回答1: You need a better understanding of RxJava

How to wait for async Observable to complete

删除回忆录丶 提交于 2020-01-01 08:22:29
问题 I'm trying to build a sample using rxjava. The sample should orchestrate a ReactiveWareService and a ReactiveReviewService retruning a WareAndReview composite. ReactiveWareService public Observable<Ware> findWares() { return Observable.from(wareService.findWares()); } ReactiveReviewService: reviewService.findReviewsByItem does a ThreadSleep to simulate a latency! public Observable<Review> findReviewsByItem(final String item) { return Observable.create((Observable.OnSubscribe<Review>) observer

How to wait for async Observable to complete

﹥>﹥吖頭↗ 提交于 2020-01-01 08:22:12
问题 I'm trying to build a sample using rxjava. The sample should orchestrate a ReactiveWareService and a ReactiveReviewService retruning a WareAndReview composite. ReactiveWareService public Observable<Ware> findWares() { return Observable.from(wareService.findWares()); } ReactiveReviewService: reviewService.findReviewsByItem does a ThreadSleep to simulate a latency! public Observable<Review> findReviewsByItem(final String item) { return Observable.create((Observable.OnSubscribe<Review>) observer