Skip to content

Instantly share code, notes, and snippets.

@aqua30
Created June 27, 2022 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aqua30/d04864afb116473a1fb73ebcdfe6b9b5 to your computer and use it in GitHub Desktop.
Save aqua30/d04864afb116473a1fb73ebcdfe6b9b5 to your computer and use it in GitHub Desktop.
private fun processTask(id: Int) {
viewModelScope.launch(Dispatchers.IO) {
try {
Log.e("Tag","thread = ${Thread.currentThread().name}")
backgroundTask(id)
} finally {
Log.e("Tag","cancelled $id")
}
}
}
private suspend fun backgroundTask(id: Int) {
val delay = id + Random.nextInt(5)
Log.e("Tag","delay for id $id is $delay")
delay(delay * 1000L)
_itemsState[id] = itemsState[id].copy(
taskStatus = TaskStatus.COMPLETED
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment