Hello :wave:, I have a question regarding `io.ktor...
# multiplatform
r
Hello 👋, I have a question regarding
io.ktor.client.HttpClient
. I am using it in a Kotlin Multiplatform project. For this, I followed the tutorial provided here: https://ktor.io/docs/getting-started-ktor-client-multiplatform-mobile.html. From what I see there, for the
HttpClient
I don't need to call
.close()
? Because the Ktor docs here https://ktor.io/docs/create-client.html#close-client describe that close needs to be used to free up the resources. However if I am calling
.close()
on my instance of
HttpClient
and after that, I am trying a 2nd request with it, I got a crash. I tried that by calling my request function from my iOS client and I run in sth like this:
kotlinx.coroutines.JobCancellationException: Parent job is Completed; job=SupervisorJobImpl{Completed}@e50024f0
. I am using one single instance in my Multiplatform module for the
HttpClient
like suggested in the Ktor docs:
Note that creating
HttpClient
is not a cheap operation, and it's better to reuse its instance in the case of multiple requests.
So there is no new instance created for each request. Is that usage correct? Can you please give me here an advice?
1
c
You should create a single instance and reuse it between all requests.
r
Yes that is what I am doing atm 👍. The question is, do I need to call
.close()
after each request on it or not? And if I need to call
.close()
I end up in a crash on iOS on a request done after
.close()
was called
c
No, you should call
close
when the app closes.
r
Thank you Ivan 👍. By app close you mean when app is terminated? So not when it goes to the background fe.?
c
When you call
close
, you can never reuse that instance again.
So if you call
close
, you have to create a new instance next time it's needed.
r
So this is a bit of contradiction here? According what you suggested Ivan and what is written in the documentation, during the whole "app live time" ideally one instance of
io.ktor.client.HttpClient
should be used right? So the suggestion to call close on this instance confuses me. Because in that case, you need to create a new instance of
io.ktor.client.HttpClient
. For me, the only logic conclusion here is, to never call
.close()
during app lifetime on the
HttpClient
to be able to reuse one instance of this
HttpClient
. So only call
.close()
be when the app terminates? But I am also wondering about this. Because when the app terminates, everything gets free up by the system automatically (at least on iOS from what I know).
c
one instance should be used
Yes. When you
close
, you destroy that instance, it doesn't count anymore. If you want to continue using it, you'll need to create a new one. You can keep the instance alive for the entire lifetime of the application if you want, or you can close it when the app goes to the background and create a new one when it comes back to the foreground.
r
Ah okay - thank you for explaining 🙂. That make sense - I would try to keep the instance alive for the entire application lifetime tbh 🙂. I hope that this does not cause any side effects.