简体   繁体   中英

How to properly combine multiple requests with rxjava2?

Is it possible with rxjava2 to achieve sequence described below? I have classes like:

class Holder {
    List<Image> images;
}
class Image {
    String url;
    String localFileUrl;
}

I need a sequence like:

* Iterate over Holders;
  * For each Holder iterate over Images;
    * Working with each images(decode Bitmap from localFileUrl and upload 
     bitmap to network -> get url for image from network -> write url to 
     Image's file url);
* After iterating over all Holders(on this step all images inside Holders
 should have filled url), make upload for Holders;

I've tried something like:

Observable.fromIterable(holders)
  .concatMap(holder -> {
    Observable.fromIterable(holder.images)
      .concatMap(image -> {
        // decode bitmap here
        uploadImage(bitmap)
      })
  })
// what should I do here to get observable which emits holders,
// not result from uploadImage(bitmap)?
  .toList()
  .flatMap(? -> uploadeHolders());

Is it possible to iterate over data, perform some action for each item on data, and do some work with prepared data (with performed action for each item)?

Something like that -

Observable.fromIterable(holders)
  .concatMap(holder -> {
    Observable.fromIterable(holder.images)
      .flatMap(image -> {
        // decode bitmap here
        uploadImage(bitmap)
        .map(url -> {
          image.url = url;
          return image;
        });
      })
      .toList()
      .map(list -> {
        holder.images = list;
        return holder;
      });
  })
  .toList(); // optional, if you need holders as a list

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