https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
t

Thomas Skovsgaard

09/09/2020, 1:15 PM
Hi, I keep getting this error
There is no event loop. Use runBlocking { ... } to start one.
from the iOS part, I use kotlin 1.4, coroutines 1.3.9 and my iOS dispatcher is based on this https://github.com/JetBrains/kotlinconf-app/blob/33f2d4e65f470d1444c5d4b46249af8feb243d03/common/src/iosMain/kotlin/org/jetbrains/kotlinconf/DispatcherNative.kt I have tried using native-mt with Dispatchers.Main but this gives me another error
Fatal exception in coroutines machinery for CancellableContinuation
The Android part works as expected. I have searched and tried a lot of solutions, but can't seem to figure out what's going wrong, any guidance is much appreciated 🙂
i

Ivann Ruiz

09/09/2020, 1:31 PM
Try using coroutines
1.3.9-native-mt
instead
t

Thomas Skovsgaard

09/09/2020, 1:53 PM
Hi Ivann, I tried using the native-mt but that gave me the
Fatal exception in coroutines machinery for CancellableContinuation
so unfortunately I'm still stuck, but thank you for the suggestion 👍
i

Ivann Ruiz

09/09/2020, 2:43 PM
are you using ktor with your setup at all?
k

kpgalligan

09/09/2020, 3:51 PM
Confirm the app is on 1.4.0 of kotlin. If not, you’ll want another example (I’d humbly suggest: https://github.com/touchlab/KaMPKit)
Yeah, looks like latest commit on main branch is Dec 2019. That’s old for KMP
😅 1
k

Kweku

09/09/2020, 5:30 PM
Grade see 1.3.9 as need than 1.3.9-native-mt so if you have any transitive dependencies on 1.3.9 thats what it will use. Try forcing it to use native-mt
t

Thomas Skovsgaard

09/09/2020, 5:42 PM
@Ivann Ruiz Yes I use Ktor This is my common dependencies, I believe they are all up to date?
Copy code
def coroutines_version = "1.3.9"
def ktor_version = "1.4.0"
def kodein_version = "7.1.0-master-88"
def serialization_version = "1.0.0-RC"
commonMain {
     
    dependencies {
        implementation(kotlin('stdlib-common'))
        implementation "org.jetbrains.kotlinx:kotlinx-serialization-core:$serialization_version"
        implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$serialization_version"
        implementation "io.ktor:ktor-client-json:$ktor_version"
        implementation "io.ktor:ktor-client-serialization:$ktor_version"
        implementation "io.ktor:ktor-client-logging:$ktor_version"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
        implementation "org.kodein.di:kodein-di:$kodein_version"

    }
}
i

Ivann Ruiz

09/09/2020, 5:44 PM
I see, had the same problem. Fixed it by downgrading Ktor to version
1.3.2-1.4.0-rc
k

kpgalligan

09/09/2020, 5:47 PM
ktor in 1.4 uses the mt version of coroutines
i

Ivann Ruiz

09/09/2020, 5:56 PM
@kpgalligan Thanks for sharing this article, been wrestling with the issue for a few days. Glad it's not just me ha!
v

Vinod Rai

09/09/2020, 6:35 PM
Hey, use native-mt and if you are using ktor logging feature disable it. It has some issue in LogLevel.All & LogLevel.Body.
👌 3
i

Ivann Ruiz

09/09/2020, 6:40 PM
@Vinod Rai You hit it right on the spot! Couldn't figure out how to get Ktor
1.4.0
to work for me, works like a charm after disabling Logging. Had it set up as
LogLevel.All
👍 1
t

Thomas Skovsgaard

09/09/2020, 7:40 PM
THANKS, changing to native-mt and ktor to loglevel.Info solved my problems, but raised a new problem, can someone explain why it's only Dispatchers.Main that works, when I try to use Dispatchers.Default nothing happens.
k

kpgalligan

09/09/2020, 7:43 PM
Ktor only works on main
m

Mgj

09/09/2020, 7:47 PM
That must be an iOS thing? You can use ktor on an IO dispatcher on e.g. android. Seems a bit odd, surely you dont want to do network calls on your main thread?
t

Thomas Skovsgaard

09/09/2020, 7:48 PM
Thanks Kevin, I had another understanding after reading this
Copy code
You don't need to implement anything with -native-mt version:
For main thread use Dispatchers.Main
For background thread use Dispatchers.Default
from https://github.com/Kotlin/kotlinx.coroutines/issues/1889
👍 1
k

kpgalligan

09/10/2020, 1:11 AM
@Mgj the main thread restriction is an iOS thing for the ktor library. Not a general iOS networking thing, obviously. Also, to be clear, ktor calls are suspend functions. You start the call from the main thread, but that suspends to do the actual network call. Networking does not block the main thread. When completed, execution resumes on the main thread with the network response
2 Views