Android 房间获取ConcurrentModificationException

Android 房间获取ConcurrentModificationException,android,android-sqlite,android-room,Android,Android Sqlite,Android Room,我在向Android表插入数据时遇到了一个问题。以下是我的Dao功能: @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(freight: Foo) @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(freights: MutableList<Foo>) 我得到的例外是: Caused by: java.util.ConcurrentM

我在向Android表插入数据时遇到了一个问题。以下是我的Dao功能:

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freight: Foo)

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freights: MutableList<Foo>)
我得到的例外是:

   Caused by: java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.next(ArrayList.java:860)
    at com.blockgrain.blockgrain.dbmanager.repository.FooRepository$insertDataInDb$1.call(FooRepository.kt:76)
我已经用同样的逻辑创建了其他表,它们工作正常,但这一个失败了。请告诉我出了什么问题

更新:

Foo.kt

 override fun get(): Observable<MutableList<Foo>> {
    val observable = getDataFromDb()
    return observable.flatMap {
        if (it.isEmpty())
            getDataFromApi()
        else
            observable
    }
}

override fun getDataFromApi(): Observable<MutableList<Foo>> {

    return WebService.createWithAuth().getFooFromWeb()
            .doOnNext {
                Logger.d(" Dispatching ${it} users from API...")
                Observable.fromCallable {
                     db.fooDao().insert(it)
                      }
                  }
                  .subscribeOn(Schedulers.io())
                  .observeOn(Schedulers.io())
                  .subscribe {
                           Logger.d("Inserted ${it} users from API in DB...")
                  }
               }
}

根据给定的代码,尚不清楚如何调用数组列表修改,从而导致:java.util.ConcurrentModificationException。 我的猜测是,一次在同一个列表上执行多个操作

dao中的insert list方法正在接受可变列表,将其更改为list,因为文件室不需要可变列表。像这样,

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freights: List<Foo>)
我建议先将数组列表复制到另一个列表,然后再对该列表执行类似的任何操作

// Before performing any operation on list
var newList:List<Foo> = ArrayList<Foo>(otherList)
// Perform operation on newList - for ex.
db.insert(newList)

如果您想同时使用ArrayList,还有另一个解决方案。但这将导致对现有in代码的重大修改。因此,我建议使用第一个选项。

根据给定的代码,不清楚如何调用数组列表修改,从而导致:java.util.ConcurrentModificationException。 我的猜测是,一次在同一个列表上执行多个操作

dao中的insert list方法正在接受可变列表,将其更改为list,因为文件室不需要可变列表。像这样,

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(freights: List<Foo>)
我建议先将数组列表复制到另一个列表,然后再对该列表执行类似的任何操作

// Before performing any operation on list
var newList:List<Foo> = ArrayList<Foo>(otherList)
// Perform operation on newList - for ex.
db.insert(newList)

如果您想同时使用ArrayList,还有另一个解决方案。但这将导致对现有in代码的重大修改。因此,我建议使用第一个选项。

您在这里为单个元素调用db.fooDao.insertit还是为Foo列表调用db.fooDao.insertit?我尝试了single和list两种方法。。两者都失败,但有相同的例外情况。您确定在访问列表时没有修改列表吗?。你能分享FooRepository的代码吗。。例外情况是在db.fooDao.insertitar您是否尝试使用此代码插入多个项?您在这里为单个元素或Foo列表调用db.fooDao.insertit的方法是什么?我尝试了single和list。。两者都失败,但有相同的例外情况。您确定在访问列表时没有修改列表吗?。你能分享FooRepository的代码吗。。在db.fooDao.insertitar处出现异常您是否尝试使用此代码插入多个项目?这是可行的,但我仍然不清楚列表在哪里使用。。不管怎样,谢谢你。很高兴这有帮助。您可能需要调试您的代码,以找到它在何处被同时使用,因为代码是被动的,我们无法直接确定它是否有效,但我仍然不清楚列表在何处被使用。。不管怎样,谢谢你。很高兴这有帮助。您可能需要调试您的代码,以找到它在哪里被同时使用,因为代码是被动的,我们无法直接确定它