Hi, I am developing a kotlin library to be used b...
# ktor
r
Hi, I am developing a kotlin library to be used both from within android and/or server side jvm. Library needs to place some http calls, hence I user Ktor Client and give to the caller the option to customize which engine will be used. My question is the following: Should a wrap usages of the ktor client with
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
{ ..... } or this is not needed by ktor?
a
This isn't needed, because the
<http://Dispatchers.IO|Dispatchers.IO>
is by default.
r
@Aleksei Tirman [JB] Thanks Aleksei! Yet another dummy question: is it default to Ktor client?
👍 1
a
I had to use withContext (Io) around my Ktor client calls (okhtttp engine ) but I'm not complaining cause I like it that way :)
a
plus1 2
r
@Aleksei Tirman [JB] Thanks for your reply. Indeed it is crystal clear now.
a
@Aleksei Tirman [JB] it's for what version ? It doesn't look like it's in latest production release
a
Since 2.3.5.
a
i see 2.3.6 branch and it doesn't show that commit https://github.com/ktorio/ktor/compare/2.3.6...main or it's not how releases are done with Ktor? also when I use 2.3.6 client in my codebase I have to wrap client calls with withContext(IO) {} otherwise it runs network calls on Main thread on Android with OkHttp engine - am I missing something?
a
You can find the commit here.
a
right so it's in main but not in 2.3.5 - do you guys release tagged main or a branch? i just don't see that commit neither in 2.3.5 nor 2.3.6 brach. And I wouldn't care except that behavior you said is fixed - I don't see that it's fixed. Again, sorry if it's confusion on my part - still digesting that turkey 🙂
a
Usually, we release from tagged main, but the few last patch releases have been released from the separate branches.
a
anyway, what is exactly fixed? I still need to wrap my calls ktorClient.get() with withContext(IO) otherwise I get ANRs - shouldn't it not be needed after this fix?
j
@Aleksei Tirman [JB] any reason OkHttp engine is not using IO?
IO was added to native in coroutines too, but ktor is not using it
a
For the network calls OkHttp uses its own dispatcher that uses an ExecutorService to run calls internally.
j
Based on the experience from @andriyo, looks like it is not a good default dispatcher as it generates ANRs on Android by default which is totally the opposite someone would expect from a network client and even more compared to the behavior of the rest of engines in Ktor