https://kotlinlang.org logo
#ktor
Title
# ktor
r

routis

11/23/2023, 1:41 PM
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

Aleksei Tirman [JB]

11/23/2023, 1:44 PM
This isn't needed, because the
<http://Dispatchers.IO|Dispatchers.IO>
is by default.
r

routis

11/23/2023, 1:53 PM
@Aleksei Tirman [JB] Thanks Aleksei! Yet another dummy question: is it default to Ktor client?
👍 1
a

andriyo

11/23/2023, 6:35 PM
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

Aleksei Tirman [JB]

11/24/2023, 10:02 AM
plus1 2
r

routis

11/24/2023, 4:44 PM
@Aleksei Tirman [JB] Thanks for your reply. Indeed it is crystal clear now.
a

andriyo

11/24/2023, 7:35 PM
@Aleksei Tirman [JB] it's for what version ? It doesn't look like it's in latest production release
a

Aleksei Tirman [JB]

11/24/2023, 7:54 PM
Since 2.3.5.
a

andriyo

11/24/2023, 8:01 PM
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

Aleksei Tirman [JB]

11/24/2023, 8:06 PM
You can find the commit here.
a

andriyo

11/24/2023, 8:33 PM
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

Aleksei Tirman [JB]

11/24/2023, 8:36 PM
Usually, we release from tagged main, but the few last patch releases have been released from the separate branches.
a

andriyo

11/25/2023, 10:02 AM
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

Javier

11/25/2023, 10:51 AM
@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

Aleksei Tirman [JB]

11/27/2023, 10:48 AM
For the network calls OkHttp uses its own dispatcher that uses an ExecutorService to run calls internally.
j

Javier

11/27/2023, 11:02 AM
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