https://kotlinlang.org logo
#coroutines
Title
# coroutines
j

Jarroyoesp

09/24/2020, 3:42 PM
Hi all, I am having some problems using KTor and coroutines in the iOS side. I am using 1.3.9-native-mt-2 but I can't create Ktor-HttpClient in the iOS side. I don't know if I am missing something. My dependencies are:
Copy code
kotlinVersion = "1.4.0"
ktorVersion = "1.4.0"
coroutineVersion = "1.3.9-native-mt-2"
serializerVersion = "1.0.0-RC"

internal actual val ApplicationDispatcher: CoroutineContext  =
    NsQueueDispatcher(dispatch_get_main_queue())

internal class NsQueueDispatcher(
    private val dispatchQueue: dispatch_queue_t
) : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        dispatch_async(dispatchQueue) {
            block.run()
        }
    }
}
But when I try to create HttpClient this exception is thrown(
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlinx.coroutines.ChildHandleNode
):
Copy code
private val client by lazy {
     HttpClient() {
         install(JsonFeature) {
             serializer = KotlinxSerializer(nonStrictJson)
         }
         install(Logging) {
             logger = Logger.DEFAULT
             level = <http://LogLevel.INFO|LogLevel.INFO>
         }
     }
}
Has anyone had the same problem? Thanks in advance.
r

ribesg

09/25/2020, 9:27 AM
Use
Dispatchers.Main
and
Dispatchers.Default
rather than custom dispatchers, now that they are available and work
Also most issues with Ktor native (the one you encountered included) are fixed in 1.4.1. The last blocking one for me is https://youtrack.jetbrains.com/issue/KTOR-1073
j

Jarroyoesp

09/27/2020, 9:00 AM
Thanks @ribesg I've tried this but it doesn't work. I've updated my dependencies to:
Copy code
kotlinVersion = "1.4.10"
ktorVersion = "1.4.1"
coroutineVersion = "1.3.9-native-mt-2"
serializerVersion = "1.0.0-RC2"
But now in iOS side when HttpClient calls get() method this exception is thrown:
kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to start one.