I'm using a coroutine to format a list.
This is how it looks
val job : Job? = null
private fun formatList(originalList:MutableList<MY_OBJECT>, callback : (MutableList<MY_OBJECT>) -> Unit){
if(job != null && !job?.isCompleted!!){
job?.cancel()
}
job = viewModelScope.launch(Dispatchers.IO) {
originalList.foreach { item ->
//do something with the item.
}
}
}
This method can be called several times during runtime, and to avoid it from doing the samething, I added a cancel call if the job isn't done yet.
The problem is, in runtime, the stuffs inside the foreach block randomly produces some index related crashes.
Why is this happening? Is this something about things going behind coroutine execution? Or are there something I don't know about the foreach loop?