List allocation trigger ConcurrentModificationException in Kotlin

49 Views Asked by At

I have a function that takes a List as a parameter and the aim is to merge this list with another list contained in an object.

private fun joinListToUpdatedList(listToJoin: List<ExpirationDatesArt>?){

        val folderUpdatedListValue : MutableList<ExpirationDatesArt>? = folderUpdatedList.ExpirationDatesArt?.toMutableList()

        if(listToJoin != null){
            if(folderUpdatedListValue != null){

                folderUpdatedListValue.addAll(listToJoin)
                val joinedUpdatedList = folderUpdatedListValue.distinctBy { it.cdean }

                folderUpdatedList.ExpirationDatesArt = joinedUpdatedList
            }
            else{
                folderUpdatedList.ExpirationDatesArt = listToJoin
            }
        }
    }

When I first call the function, my object's list is null, so its value is initialized with listToJoin.

If I call the function a second time, whether immediately or several seconds later, the folderUpdatedListValue initialization crashes and returns a ConcurrentModificationException error.

I don't understand why, because I'm not deleting an item, I'm just trying to access the list of the object I'm interested in.

I tried to use addAll function on listToJoin with folderUpdatedListValue but the result is the same.

How can I enable this function to be called several times?

0

There are 0 best solutions below