rxjava 后台进程和中间更新 ui

rxjava background process and update ui intermediately

一个冗长的后台操作,它有 FOR 循环。循环中的每次迭代完成后,我需要更新 UI。我怎样才能在 RX java 中实现这一点,而不用等待所有迭代完成或编写 2 个 observable?

doInBackGround() {

       //step 1
       List<Object> list= < time consuming logic >

       //step 2
       for(Object item: list) {
          < time consuming logic >
          updateUi();
       }

}

可以使用 AsyncTask/Threads 完成。但我正在试验 RX

我认为您正在从一个长 运行 操作中获取一个列表,您必须遍历该列表并对列表中的每个项目再次执行一个长 运行 操作。同时,当每个项目完成长 运行 任务时,您必须更新 UI。如果你正在尝试这样做,你可以尝试

 Observable.fromCallable {
          val list = timeConsumingLogic()
          list // return the list
        }.flatMap { source: List<Any>? -> Observable.fromIterable(source) } // iterate through each item
            .map { item: Any? ->
                 // perform time consuming logic on each item
                item 
            }
            .observeOn(AndroidSchedulers.mainThread()) // do the next step in main thread
            .doOnNext { item: Any? -> 
                // perform UI operation on each Item 
                 }
            .subscribeOn(Schedulers.io()) //  start the process in a background thread
            .subscribe()