2

I'm scratching my head here trying to understand why I'm getting a ConcurrentModificationException here...

This is my code:

var adapterCurrentList : PagedList<WorkPackagesQuery.WorkPackage>? = null
val workPackagesArrayList = arrayListOf<WorkPackagesQuery.WorkPackage>()

fun turnPagedListIntoArrayList(searchString: String) {
    workPackagesArrayList.clear()
    val tempCurrentArray = Collections.synchronizedList(adapterCurrentList)
    synchronized(tempCurrentArray) {

        for ((index, workPackage) in tempCurrentArray.withIndex()) { //exception is thrown here. 
            if (workPackage.id().contains(searchString)) {
                workPackagesArrayList.add(workPackage)
            }
        }
    }

}

What am I missing? I am not modifying tempCurrentArray while iterating over it... First time through, with searchString, it runs fine, the second time, with a different searchString, it throws the exception.

Here is the stacktrace:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bechtel.pf.mock, PID: 18686
java.util.ConcurrentModificationException
    at java.util.ArrayList$SubList.size(ArrayList.java:1057)
    at androidx.paging.PagedStorage.get(PagedStorage.java:170)
    at androidx.paging.PagedList.get(PagedList.java:410)
    at java.util.AbstractList$Itr.next(AbstractList.java:358)
    at com.bechtel.pf.ui.workpackages.WorkPackagesViewModel.turnPagedListIntoArrayList(WorkPackagesViewModel.kt:48)
    at com.bechtel.pf.ui.workpackages.WorkPackagesViewModel.pareDownArrayForSearch(WorkPackagesViewModel.kt:59)
    at com.bechtel.pf.ui.workpackages.WorkPackagesFragment$onCreateView$$inlined$addTextChangedListener$1.afterTextChanged(TextView.kt:101)
Onik
  • 19,396
  • 14
  • 68
  • 91
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106

0 Answers0