touchlab / KaMPKit

KaMP Kit by Touchlab. A collection of code & tools designed to get your mobile team started quickly w/Kotlin Multiplatform
https://touchlab.co/
Apache License 2.0
2.2k stars 199 forks source link

FreezingException -- Question #158

Closed BJN373 closed 3 years ago

BJN373 commented 3 years ago

FreezingException on iOS

Hi, thanks for sample - it is really helpful. I consider Kotlin Multiplatform for rewriting existing project. So i did some tests with your KaMPit

I face one issue and hope that you help. I customized your sample for testing my needs - basically just added some additional request and some logs :

Screen Shot 2021-01-05 at 9 10 52 PM

That issue I was not able to solve. In official documentation and (article)[https://medium.com/@kpgalligan/ktor-and-kotlin-native-fb5c06cb920a] from @kpgalligan it is mention that we need to initiate Ktor calls from the main thread.

--

My questions if there any possibility to do request(or move it) out from the main thread ? Probably I am doing something wrong(but on Android works fine) - freezing UI on iOS is not really acceptable for me. Thanks in advances for you answer.

kpgalligan commented 3 years ago

Ktor needs to be called from the main thread. The network request itself happens from a background thread, but on return, parsing would happen in the main thread, and if you have significantly large/complex data, that is a problem.

In general, we do one of two things. 1) Have ktor just return the string, then parse it in your own code in a different thread, or 2) replace ktor with a platform-specific implementation of calling the network.

In your case, I would suggest just getting the string from ktor, then parse it in a different thread.

BJN373 commented 3 years ago

Thank you.