0

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?

1 Answer 1

1

Making your code cancellable

For job.cancel() to work properly, you need to make your code inside coroutine cancellable.

job = viewModelScope.launch(Dispatchers.IO) {
      originalList.foreach { item ->
           if(!isActive) return@launch
           //do something with the item.
      }
      if(!isActive) return@launch
      // do something
}

Here the line if(!isActive) return@launch is checking if the coroutine is still active or not. Checking for isActive before and after computationally intensive code is good practice.

Note: if you are not doing any network or database calls, always try to use Dispatchers.Default which is optimized for computation instead of Dispatchers.IO.

Sign up to request clarification or add additional context in comments.

3 Comments

One more question. Does this mean that the foreach block will not stop even if the job itself has been cancelled?
yes, it will not stop automatically. we have to manually close it depending upon isActive.
Thanks. Manually stopping the loop did the trick. By the way, the logic inside the foreach block consists of heavy actions such as comparing every item's Date value several times. Will Dispatchers.Default block the UI?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.