These are chat archives for ReactiveX/RxJava

14th
May 2018
defaultbr
@defaultbr
May 14 2018 14:10

Hello, someone know how to make this work syncronously?:

  .subscribeOn(scheduler.computation())
                .observeOn(scheduler.ui())
                .flatMap { ids -> Observable.fromIterable(ids) }
                .flatMap { id -> requestEmployeeData(id) }
                .doOnNext { response -> println("response ${response}") }
                .subscribe()

the requestEmployeeData is a retrofit request that returns Observable<Response<SyncEmployeeData>>, the issue is that its creating more than 100 requests at the same time but i only want to start the next on the ids list after the doOnNext is processed, is that possible?

Ignacio Baca Moreno-Torres
@ibaca
May 14 2018 14:52
I don't understand, do you want to resolve all ids, only the first, the last or just one each time sequentially? Anyway the UI shceduler should be added after the retrofit flatmap and only if actually need to use the UI thread, not required for the println
defaultbr
@defaultbr
May 14 2018 15:02
@ibaca i have a list of 100 ids, i want do iterate for each one and do the request, but i want the request to be made in a sync way (not async). Just start the request for id=2 when the request of id=1 is resolved, make sense ? i need this behaviour to not """ddos the server """
defaultbr
@defaultbr
May 14 2018 15:21
tried the blockingFirst but only works when the app is open and visible (its running in a service)
Adam Dohnal
@dohnala
May 14 2018 15:22
@defaultbr what about trying concat with map instead of flatMap? Something like this
        Observable.concat(
                Observable
                        .fromIterable(ids)
                        .map(id -> requestEmployeeData(id))
                        .subscribeOn(Schedulers.computation()))
                .doOnNext(response -> println("response ${response}"))
                .subscribe();
defaultbr
@defaultbr
May 14 2018 16:28
will try
defaultbr
@defaultbr
May 14 2018 16:34
@dohnala cool, this worked!!!! thanks
Ivan Schütz
@i-schuetz
May 14 2018 21:10
is there a way to send 2 consecutive error events to a subject?
basically I want that it continues normally when there are errors
Ivan Schütz
@i-schuetz
May 14 2018 21:16
of course given that any observers of it recover with onErrorResumeNext, onErrorReturn, etc
Ivan Schütz
@i-schuetz
May 14 2018 21:21
mySubject
    .onErrorResumeNext(Function {
        println("caught")
        Observable.empty<Item>()
    })
    .subscribe()

mySubject.onError(Throwable(""))
mySubject.onError(Throwable(""))
Ivan Schütz
@i-schuetz
May 14 2018 21:28
the use case... is that I forward the results of some observables to this subject and want to let the observer of the subject decide how to handle the errors
Ivan Schütz
@i-schuetz
May 14 2018 21:37
the only solution I can imagine right now is to create a separate subject for throwables and subscribe to it to get the error stream... which is sent via onNext(throwable) instead of onError(throwable)... but this is a bit ugly