我有问题。解决方法可能很简单,但我的头太过热了……
我想在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)
})
})
}
建议的解决方法
Observable.merge(workers.map {
deleteOldWorkers(it.event_planner_id!!)
})
答案 0 :(得分:1)
Observable.merge(workers.map {deleteOldWorkers(it.event_planner_id !!)})。doOnComplete()也许?