RxJava ConcurrentModificationException

时间:2018-07-04 08:53:43

标签: java android kotlin rx-java rx-android

我在RxJava中是菜鸟,但是有一天...; )

我所拥有的:

  • 使用事件计划器模型接收json
  • 在此模型内是任务
  • 我想将此任务保存到本地数据库中,但也希望在有关系的情况下将它们添加为parent_id

有更多经验的人可以看看下面的代码:

    private fun saveEventPlanners(eventPlanners: ArrayList<EventPlanner>) {
    LogMgr.d(TAG, "saveEventPlanners() events:$eventPlanners")

    compositeDisposable.add(wfmStorageDomain.saveEventPlanners(eventPlanners)
            .subscribeOn(rxSchedulers.computation())
            .observeOn(rxSchedulers.computation())
            .subscribe({ saved ->
                LogMgr.v(TAG, "event planners saved successfully, value: $saved")

                val taskList = ArrayList<Task>()

                eventPlanners.forEach { eventPlanner ->

                    if (eventPlanner.status == EventPlanner.EventPlannerStatus.NEW) {
                        FS.get().wfmComponent.getEventPlannerStatusChanger().updateEventPlannersStatus(eventPlanner.event_id!!, EventPlanner.EventPlannerStatus.RECEIVED)
                    }

                    (eventPlanner.tasks as ArrayList<Task>).forEach {
                        val task = Task()
                        task.id = it.id
                        task.status_id = Task.STATUS.NEW
                        task.name = it.name
                        task.end_scenario_id = it.end_scenario_id
                        task.start_scenario_id = it.start_scenario_id
                        task.isBind = it.isBind
                        task.parent_event_planner_id = eventPlanner.event_planner_id

                        taskList.add(task)
                    }
                    compositeDisposable.add(wfmStorageDomain.saveTasks(taskList)
                            .subscribeOn(rxSchedulers.computation())
                            .observeOn(rxSchedulers.computation())
                            .subscribe({
                                LogMgr.d(TAG, "tasks saved successfully = $it")
                            }, {
                                LogMgr.e(TAG, "Error while saving tasks", it)
                            }))

                }
            }, {
                LogMgr.e(TAG, "Error while saving event planners", it)
            }))

    notifyObservers(eventPlanners)
}

此函数出现错误:

java.util.ConcurrentModificationException
    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
    at com.raizlabs.android.dbflow.sql.saveable.ListModelSaver.saveAll(ListModelSaver.java:32)
    at com.raizlabs.android.dbflow.sql.saveable.ListModelSaver.saveAll(ListModelSaver.java:19)
    at com.raizlabs.android.dbflow.structure.ModelAdapter.saveAll(ModelAdapter.java:196)
    at com.raizlabs.android.dbflow.rx2.structure.RXModelAdapter$3.call(RXModelAdapter.java:61)
    at com.raizlabs.android.dbflow.rx2.structure.RXModelAdapter$3.call(RXModelAdapter.java:58)
    at io.reactivex.internal.operators.completable.CompletableFromCallable.subscribeActual(CompletableFromCallable.java:35)
    at io.reactivex.Completable.subscribe(Completable.java:1919)
    at io.reactivex.internal.operators.completable.CompletableSubscribeOn$SubscribeOnObserver.run(CompletableSubscribeOn.java:64)
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:579)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:841)

这是保存到db中的样子:

    override fun saveTasks(tasks: ArrayList<Task>): Single<Boolean> {
    LogMgr.d(TAG, "saveTasks() : $tasks")
    return Single.create({ emitter ->
        RXModelAdapter.from(Task::class.java)
                .saveAll(tasks)
                .subscribeOn(getSubscriptionScheduler())
                .subscribe({
                    LogMgr.d(TAG, "saveTasks() onComplete")
                    emitter.onSuccess(true)

                }, {
                    LogMgr.e(TAG, "saveTasks() onError ", it)
                    emitter.onError(it)
                })
    })
}

  private fun getSubscriptionScheduler(): Scheduler {
    return FS.get().commonComponent.rxSchedulers.get().sqlite()
}

1 个答案:

答案 0 :(得分:4)

问题是这样的:

val taskList = ArrayList<Task>()

eventPlanners.forEach { eventPlanner ->

    // ...

    compositeDisposable.add(wfmStorageDomain.saveTasks(taskList)

    // ...
}

您有一个列表,可以继续从其他eventPlanner项中添加该列表,并将该部分列表提交到保存任务。如果eventPlanners中有多个项目,将导致ConcurrentModificationException错误。将taskList放在forEach循环的本地,或者将saveTasksforEach中移出。