Closed BJN373 closed 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.
Thank you.
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 :
I added some logs to print threads - and noticed that storing data in db works in background thread - which is fine. My issue with ktor client on iOS. I noticed that UI is actually locked in a moment of doing request (or parsing it) - especially when response is quite big and complex object. Ktor is doing request on main thread - which is not ok for me because freezing UI even less then 1s is not nice user experiences. So I tried to move request for iOS from main thread with different ways. And was not able to do it. As an example I just wrapped calls into "withContext" and switch "Dispatcher" to "Default" - and it worked for Android. On another hand I got some error on iOS side(as on screenshot) - always same Error with different approaches.
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.