RxJava2. Execute a request for every item in a list

rxjava list of items
android rxjava list to items
observable.fromiterable example
rxjava iterate through list
rxjava flatmap
rxjava observable foreach example
observable fromiterable flatmap
observable from arraylist

I have a list List<FileModel>

FileModel is just a class contains id: Int

id - is ID of photo file I need to fetch and cast to Bitmap

I have a request:

fun getFile(fileId: Int): Single<ResponseBody>

This request returns ResponseBody which we can cast to Bitmap

And

fun generatePhoto(responseBody: ResponseBody): Single<Bitmap?>

What I want is to create a function

fun getPhotos(list: List<FileModel>): Single<List<Bitmap>> {
    // Execute getFile(...) for each item in the list
    // Cast getFile(...) result to Bitmap using generatePhoto(...)
    // Return a list of Bitmaps
}

I've tried something like this but it's completely wrong

fun getPhotos(list: List<FileModel>): Single<List<Bitmap>> {
    return Observable.fromIterable(list)
        .flatMap { getFile(it.id) }
        // How to call generatePhoto(...) here?
}

You can do it like this:

fun getPhotos(list: List<FileModel>): Single<List<Bitmap>> {
    // Get an Observable of the list
    return Observable.fromIterable(list)
        // Get a Single<ResponseBody> for every FileModel
        .flatMapSingle { getFile(it.id) }
        // Get a Single<Bitmap> for every ResponseBody
        .flatMapSingle { file -> generatePhoto(file) }
        // Put everything back on a list
        .toList()
}

This way you can iterate over the list flapMapping for your needs and then putting it back together as a list in the end. The toList() operator is just a convenience that puts together the items emitted previously.

And to call this function just go:

    getPhotos(list)
        .doOnSuccess { resultList ->
          Log.d("Rx", "doOnSuccess.resultList=[$resultList]")
        }
        .subscribe()

By the way, if you have RxKotlin as a dependency you can get an Observable from a List with an extension function, like this: myList.toObservable()

android - RxJava2. Execute a request for every item in a list, You can do it like this: fun getPhotos(list: List<FileModel>): Single<List<Bitmap>> { // Get an Observable of the list return Observable. SubscribeOn is the method in which we tell do the task to run in a background thread by giving Schedulers object. The following example shows how we can make a network request with observable. Observable<User> userObservable = Observable.fromCallable(() -> client.getUser(request.execute())); This is how you can make a network request with

Observable.fromIterable(list) should emit an event for each item in the list which means that you're return type in getPhotos be Flowable<Bitmap> as Flowable can return more than one value.

I think what you actually want is:

fun getPhotos(list: List<FileModel>): Flowable<Bitmap> {
    return Observable.fromIterable(list)
        .map { getFile(it.id) }
        .map { generatePhoto(it) }
}

if you want to emit a list of Bitmaps you can use single like this

fun getPhotos(list: List<FileModel>): Single<List<Bitmap>> {
    return Single.just(list)
        .map { it.map { getFile(it.id) } }
        .flatMap { 
            Single.just(it.map { generatePhoto(it) }) 
        }
}

Observable<List<X>> to Observable<X> that emits each element in , Successfully merging a pull request may close this issue. None yet. 8 participants​. @jondwillis · @JakeWharton · @davidmoten · @zsxwing · @  Sending some information for each item in list (eg. sending ‘like’ message for each post in the list). To make sure that every request will be called, we definitely should not use switchMap here.

Have you tried:

fun getPhotos(list: List<FileModel>): Single<List<Bitmap>> {
    return Observable.fromIterable(list)
        .flatMapSingle { getFile(it.id) }
        .flatMapSingle { generatePhoto(it) }
}

?

The missing RxJava 2 guide: Supercharge your Android , RxJava 2, which was released some time back, featured a major rewrite of the When you need to perform multiple asynchronous operations, and each of Observables can emit items only when someone subscribes to them. Check out this page for a complete list of all the ways in which you can create an observable. If, for some reason, you’d like to request for new data from the API every 10 minutes, you can do it with RxJava in just one line: sourceObservable.repeatWhen(completed - > completed . delay( 10 , TimeUnit .

How to make complex requests simple with RxJava in Kotlin, Possibly your app needs to make multiple requests, that wait for each other, on each other, we will get the best results if we execute them in parallel and then the take with map, the transformation will be applied to every element in the list,  Look at this log output. When the view is detached, several items are emitted by the source, but not delivered. This is correct so far. After the view is reattached, all of the items are delivered again, including those which had already been delivered (c9915aa) and those which should

How to fetch REST API every 2 seconds with RxJava 2, During my work on one of my Android application I've stomped on a Here is a list of tools that I used with short explanation what it does. RxJava 2 & Retrofit — both libraries are used to perform API call and make it asynchronous. create Logger object and implement CommandLineRunner interface,  Just Build the project and start learning RxJava by examples. RxJava 2.0 has been completely rewritten from scratch on top of the Reactive-Streams specification. The specification itself has evolved out of RxJava 1.x and provides a common baseline for reactive systems and libraries. Because Reactive

Using RxJava 2 - Tutorial, An observable may emit any number of items (including zero items). Reactive programming provides also a simplified way of running Maybe<List<Todo>> todoMaybe = Maybe.create(emitter -> { try the application, without the need to do an such an expensive call all the time a subscription is added. However, I'd like to be able to force serial execution for these Singles, but I don't know the next Single at this time, so I cannot concat() them before subscribe().. How would I approach forcing a "one-at-a-time" execution, and buffer the Singles until this particular Single is executed?

Comments
  • I'm not an expert in Rx, could you provide an example?
  • Actually, it's probably just supposed to be flatMap.
  • Thank you for the answer.
  • But if we use Single.just(list) inside map { } we will have List<FileModel> instead of FileModel
  • i've updated the answer, you can iterate over each list inside of the map block
  • you can flatMap to evaluate the single but you have to emit another single in that case
  • Thank you for the answer.
  • We can't call flatMapSingle after flatMap
  • You're right, maybe it should be flatMapSingle on both.
  • Thank you for the answer.