Grettings, could someone help me with this, I've already implemented an aux list to add the elements that came from an observer, but still the problem persist, the ConcurrentModificationException
is happening on the addAll line.
Is inside a Fragment Class, an I had an observer that cames from a manager the project has that provides a list of element that will be displayed on an arraylist
manager.getMutableList()
.observe(viewLifecycleOwner, Observer { mutableList ->
val auxList = mutableListOf<CustomClass>().apply {
addAll(mutableList)
}
if (auxList.isNotEmpty()) {
...
} else {
...
}
(recyclerView.adapter as RecyclerAdapter).run {
items = auxList
notifyDataSetChanged()
...
}
})
This is the stack trace
java.util.ArrayList$SubList.size (ArrayList.java:1057)
java.util.ArrayList.addAll (ArrayList.java:588)
com.project.project.Fragment$setObservers$6.onChanged (Fragment.java:330)
com.project.project.Fragment$setObservers$6.onChanged (Fragment.java:74)
androidx.lifecycle.LiveData.considerNotify (LiveData.java:131)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1101)
Any help would be appreciated!
The problem is that you modify your mutableList
from a thread other than your main thread. The best way too avoid these exceptions is to make your list immutable (so just a List
instead of MutableList
).
All code that wants to modify it then needs to make an own copy:
val copy = list.toMutableList()
copy.add(...)