Search code examples
javaandroidkotlinrx-javarx-kotlin

Perform a task for all items in a list and then perform another task in RxJava


Unfortunately, my REST Delete operation work only for one item. So what I was trying to do is,

Observable.just(items).flatMapIterable { items -> items }.flatMap {
                //call REST DELETE for every item
            }.flatMap {
                // call REST GET
            }

The problem is the GET call is being called for every item. How can I wait for finishing all the delete done and then perform the GET call?

Thanks in Advance.


Solution

  • In your case, you can apply toList() like this

    fun doTask(items: List<String>):Observable<Boolean>{
            return Observable.fromIterable(items)
                    .flatMap { processItem(it) }
                    .toList()
                    .toObservable()
                    .flatMap { finalTask() }
        }