简体   繁体   中英

Best way to get List from Observable in Rxjava

I'm just exploring Rxjava in one of my android application, and got stuck at one place, honestly speaking I'm very new to this library so don't mind if my question frustrate someone;-)

So I'm trying to access the Room Database using RxJava where I'm returning the Observable List, once I get this Observable I'm trying to use map operator to get a list of ids & query again the database, which again returns me the Observable List but the map operator expects List as a return type. How can I tackle this please suggest?

Below is the code snippet:

  private void getAllPcbs() {
    isLoading.setValue(true);
    getCompositeDisposable().add(
            getRepositoryManager().loadAllPcbDetails()
                    .flatMap((Function<List<PcbDetails>, ObservableSource<?>>) pcbDetails -> {
                        List<Long> pcbList = new ArrayList<>();
                        for (PcbDetails details : pcbDetails)
                            pcbList.add(details.getPcbId());
                        return getRepositoryManager().loadAllPcbs(pcbList);
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this::onSuccess, this::onError)
    );
}

private void onError(Throwable throwable) {
    isLoading.setValue(false);

}

private void onSuccess(Object o) {
    isLoading.setValue(false);
    pcbList.setValue((List<Pcb>) o);
}



public interface DbHelper {
    Observable<List<PcbDetails>> loadAllPcbDetails();
    Observable<List<Pcb>> loadAllPcbs(List<Long> pcbIdList);
}

Go like

        getRepositoryManager().loadAllPcbDetails()
            .flatMapIterable {
                    listPcbDetail-> listPcbDetail
                // listPcbDetail is ArrayList<PcbDetails>
                // Converts your list of ids into an Observable
                // which emits every item in the list           
            }
            .flatMap { pcbDetail ->
                // pcbDetail is PcbDetails
                getRepositoryManager().loadAllPcbs(pcbDetail.pcbIdList)
            }.subscribe { listPcb ->
                // listPcb is ArrayList<Pcb>
            }

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