https://kotlinlang.org logo
#coroutines
Title
# coroutines
i

igor.wojda

03/18/2021, 11:05 AM
What are the ways to fix this problem ?
w

wasyl

03/18/2021, 11:27 AM
wrap the call with
withContext
using IO dispatcher
i

iseki

03/18/2021, 2:09 PM
fromJson
read data from
bufferedSource
might block the depended thread. Maybe nonblocking parse mode will be useful. (If the json deserializer has this feature.)
p

pawegio

03/18/2021, 2:59 PM
extract the expression to non-suspend function or remove
suspend
modifier
the reason is that
fromJson
don’t care about coroutines, it’s a blocking method call that ignores coroutine cancellation
u

uli

03/18/2021, 5:45 PM
@wasyl @iseki why did you strike your text? Isn’t the ide telling us that we should move this call to background:
Copy code
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
  .....fromJson(…)
}
w

wasyl

03/18/2021, 5:52 PM
Yep I got confused for a while here
👍 1
u

uli

03/18/2021, 5:55 PM
@igor.wojda you should always choose the appropriate dispatcher as close to the ' reason’ as possible. Do not set Dispatchers.IO at the beginning of the call chain. There you can should not know which dispatcher is the right one. Set it just around the method that really does blocking stuff
3 Views