How to update the first observable with the data of the second dependent on the first

29 Views Asked by At

In Android using Retrofit combined with RxJava I get an object Observable. Inside I have a List; inside Table I have properties containing a foreign key from the object Product. My goal is to get the ServiceProductBOM.List and for each Table with a foreign key I want to get Observable and update a field in Table with data from Product so that I have i.e. product code along with its key. At the end of the process I want to pass List to an Adapter. I would like to know how to develop the reactive part.

I tried something like this:

class ServiceProductBOM {
    properties....
    List<Table>

    // getters and setters
}

class Table {
    properties...
    productForeignKey
    productCode // empty to be updated later

    // getters and setters
}

class Product {
    properties...
    key
    productcode
    ....

    // getters and setters
}


retrofitWSClient.getServiceProductBOM(...)
.flatMapIterable(bom -> bom.getTable())
.flatMap(row -> retrofitWSClient.getProduct(...)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());

Once subscribed I do not know how to update the Table properties with properties from Product and keep trace to the end of process.

1

There are 1 best solutions below

0
Favrosc On

In the end I did something like this:

Observable<Table> observableTable = Observable.fromIterable(bom.getTable());
Observable<Product> productObservable = observableTable
    .doOnNext(table -> {
        rowTemp = table;
    })
    .flatMap(table -> retrofitWSClient.getProduct(...))
    .doOnNext(product -> {
        rowTemp.setProductCode(product.getProductCode());
        updatedBom.add(rowTemp);
    })

Once subscribed productObservable and completed I find updatedBom filled in with all rows updated with data from Product.