简体   繁体   中英

How to process each iteration of list of items in rxjava with retrofit in Android

I am a newbie in RxJava programming and I want to ask about how to process each iteration of list that I have from a retrofit call. Basically what I want to do is:

  1. Get list of appointments
  2. Iterate each appointment and get the patient_id and the subservice_id
  3. Convert back to list of appointments

Here is my code so far

Observable<List<Appointment>> callAppointments = appointmentServiceRx.getService().getConfirmedAppointments(user_id);
    callAppointments.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .flatMapIterable(apps -> apps)
            .concatMap(app -> getPatientById(app)
                    .doOnNext(this::addPatientNames)
                    .map(patient -> app)
            ).concatMap(app -> getSubserviceById(app)
                    .doOnNext(this::addSubserviceNames)
                    .map(subservice -> app)
            ).toList()
            .subscribe(res -> doStuff(),
                    throwable -> showThrowable());

Please help and thank you in advance...

The observeOn(AndroidSchedulers.mainThread()) call right at the beginning forces all subsequent operators to run on the main thread, which then throws NetworkOnMainThreadException .

Try moving this call to the last point before the subscribe :

// ...
.observeOn(AndroidSchedulers.mainThread())
.subscribe(res -> doStuff(),
    throwable -> showThrowable());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM