Hello guys, I'm developing an app for iOS/Android ...
# multiplatform
m
Hello guys, I'm developing an app for iOS/Android using a KMM shared library. I've an authStateListener with a callback and it works fine - but now I need to make a httpRequest inside this callback when the token expires. When I try to use
runBlocking
in KMM it works fine on android, but it crashes on iOS. In short i just need to run an async function in a non-async one:
Copy code
fun getMyToken() {
    return settings.getOrNull("ID_TOKEN") ?: runBlocking {
        return@runBlocking getMyTokenAsync()
    }
}

suspend fun getMyTokenAsync(): String {
    return httpRequest...
}
Is it possible inside the shared library?
p
Can you post the crash stacktrace if available? You can use a coroutineScope and just call launch/async. What limits you to have a coroutine scope in your service class ?
m
I'm calling the callback from a non-suspend function, and I can't change it because it comes from FirebaseAuth. I can explain better if you want, but there's no way to call a suspend function inside a non-suspend one? Instead a run-blocking, it could be a completion callback for example
On iOS, actually, it doesn't crash. It indeed blocks the thread to execute the function but never returns. I keep waiting for the script to continue, but it never happens.
p
Ah I see, I would say try creating your own coroutine scope instance. I don't know what could be wrong in this case. Are you sure your service is returning? Not hanging around
m
Thanks! I did it creating a custom coroutine scope using
CoroutineScope(Dispatchers.Main + Job())
. However, I still need to use a callback to know when suspend/async task finishes.
p
If you don't want to change your service layer to be a suspend function then use suspendCancelableCoroutine for a one single call type of call or callbackFlow if you using a websocket type of connection. It seems that all you need is
suspendCancelableCoroutine