RxJava调用方法When Loop将完成迭代

时间:2018-10-10 11:58:33

标签: android kotlin rx-java rx-android

我有问题。解决方法可能很简单,但我的头太过热了……

我想在forEach循环完成工作之后调用方法ONCE。

感谢任何示例解决方案!

 override fun saveWorkers(workers: ArrayList<Worker>): Single<Boolean> {
    LogMgr.d(TAG, "saveWorkers() : $workers")

    // remove old workers for current Event Planner and save new
    workers.forEach {
        deleteOldWorkers(it.event_planner_id!!)
                .subscribeOn(getSubscriptionSchedulerForSave())
                .subscribe({ status ->

                }, { error ->

                })
    }

    return Single.create({ emitter ->
        RXModelAdapter.from(Worker::class.java)
                .saveAll(workers)
                .subscribeOn(getSubscriptionSchedulerForSave())
                .subscribe({
                    LogMgr.d(TAG, "saveWorkers() onComplete")
                    emitter.onSuccess(true)

                }, {
                    LogMgr.e(TAG, "saveWorkers() onError ", it)
                    emitter.onError(it)
                })
    })
}
  1. 建议的解决方法

        Observable.merge(workers.map {
        deleteOldWorkers(it.event_planner_id!!)
    })
    

enter image description here

1 个答案:

答案 0 :(得分:1)

Observable.merge(workers.map {deleteOldWorkers(it.event_planner_id !!)})。doOnComplete()也许?