Manoj Perumarath
Manoj Perumarath

Reputation: 10254

How to correctly use withIndex with takeUntil ,forEachWhile, with RxJava?

In my expression, even takeUntil is true/false, subscribe method is emitting all the list items, which should stop once takeUntil is true right?

Observable.fromIterable(countryResponseList.withIndex())
  .subscribeOn(Schedulers.io())
  .observeOn(Schedulers.io())
  .takeUntil((index,item) -> item.getId() == currentCountryCodeId)
  .subscribe((index,item) -> run{
     Log.d("items and index", " ${item} and ${index}")
  })

What i need is to get the current index, when the expressions match( item.getId() == currentCountryCodeId) each other and the RxJava expression should stop continuing. I don't know how to do that, any help appreciated. I also found forEachWhile, will that suit this situation?

Upvotes: 1

Views: 300

Answers (1)

MinseongPark
MinseongPark

Reputation: 342

I'm not sure you want to stop iterating countryResponseList.withIndex(). If so, the upstream should be run on the same thread with takeUntil.

Hope underlying is what you want. If not, how about describe more in detail?

Observable.fromIterable(countryResponseList.withIndex())
  .takeUntil((index,item) -> item.getId() == currentCountryCodeId)
  .subscribeOn(Schedulers.io())
  .observeOn(Schedulers.io())
  .subscribe((index,item) -> run{
     Log.d("items and index", " ${item} and ${index}")
  })

Upvotes: 1

Related Questions