Hey there! So a colleague tried out the ktor call ...
# kotlin-native
m
Hey there! So a colleague tried out the ktor call I implemented in the
SharedCode
framework, but he got a crash with EXC_BAD_ACCESS. So what happened here? It looks like a bug in coroutines, but I don’t know. How do we debug crashes like this, and most importantly: how do we fix it?
k
message has been deleted
Coroutines in native can’t be used between multiple threads. You can use Ktor to make network calls, but you have to be careful in some cases. What does the code look like?
m
It’s basically a runBlocking around a ktor call
and the whole thing is called from a background thread
This is our approach, while
runBlocking
is an
expect fun
that uses the actual native implementation of the same name
so what’s a good pattern?
and still, it would be nice to know how to debug these forms of crashes…
k
On my phone now so hard to comment
Debugging that one would be tough. The native Ktor client for iOS expects to return to the main thread. I would assume that's ultimately what's causing that issue. Seeing the source code for that if tricky because of how the compiler embeds file info (I assume). For your code you'd have the source open
I think there's a need for a sync mode network library. Ktor kind of expects to suspend
m
Hmm, so call it from the main thread then I guess
k
More source context might help. Also wondering if any log info printed below?
m
can the shared code then simply expose
suspend
functions?
k
No. To get back to ios you'd need see form of callback. I use Swift lambdas, generally
👍 1
This is where an alternate networking lib would be good. If you want to manage concurrency in iOS rather than kotlin (which many teams do)
m
but to get around the limitation of not having
suspend
functions here, I assume I will have to use
runBlocking
anyway
k
No. Take a callback lambda, launch coroutine in kotlin. The call will return to ios, suspend in kotlin. When resumed, the lambda is called in main thread
m
that sounds like the way to go
any good example code on this one?
k
Again, on phone. Nav is hard
m
no problem - I’ll check back tomorrow 🙂
thanks for all the help, I really appreciate it!