khairil.ushan
03/02/2022, 7:06 PM@Throws(
CancellationException::class,
UnauthorizedError::class
)
suspend fun login() {
runBlocking {
val fakeWaitingTime = 300L
delay(fakeWaitingTime)
}
}
Then if I try to execute that login function inside Swift concurrency context like this
Task {
try? await Auth().login()
}
It will crash with this error:
Function doesn't have or inherit @Throws annotation and thus exception isn't propagated from Kotlin to Objective-C/Swift as NSError.
It is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared
I am using this version of coroutines
const val coroutines = "1.6.0-native-mt"
Is there any work around for this? Thank you.Joffrey
03/02/2022, 7:08 PMrunBlocking
should not be used inside suspend functions. Why do you need it here?khairil.ushan
03/02/2022, 7:09 PMkhairil.ushan
03/02/2022, 7:21 PMIt is considered unexpected and unhandled instead. Program will be terminated.
Uncaught Kotlin exception: kotlin.IllegalStateException: There is no event loop. Use runBlocking { ... } to
Joffrey
03/02/2022, 7:23 PMrunBlocking
from the most top-level place that calls suspend functions, but not from inside suspend functions themselveskhairil.ushan
03/02/2022, 7:32 PMkotlin.native.IncorrectDereferenceException
is due to the runBlocking ?Joffrey
03/02/2022, 7:44 PMsuspend
. You should remove the suspend
modifier, and use runBlocking
to call suspend functions in a blocking wayJoffrey
03/02/2022, 7:45 PMkhairil.ushan
03/02/2022, 7:45 PMRick Clephas
03/03/2022, 5:22 PMnative-mt
version is being used. Could you try to force the usage of the native-mt
version as described here?Rick Clephas
03/03/2022, 5:27 PMI’m not very well versed in Kotlin/Native, but if it’s called from platform side, then I think it shouldn’t be suspend . You should remove the suspend modifier, and use runBlocking to call suspend functions in a blocking wayYou can actually use
suspend
functions from ObjC/Swift. These suspend
functions are exported to ObjC as completion handler functions. Swift will in turn “import” those as async
functions.
Though it’s import to keep in mind that the actual function isn’t a real Swift async
function, meaning it doesn’t support cancellation.
If you would like to use suspend
functions from Swift, you can take a look at my library KMP-NativeCoroutine which solves the limitations like missing cancellation support.Joffrey
03/03/2022, 5:31 PMkhairil.ushan
03/04/2022, 5:54 AMclass Authenticator {
private val store: AuthStore by inject()
suspend fun login() {}
}
then I try to use it in my Swift code like this
Task {
do {
try await Authenticator().login
} catch {}
}
Since code inside Task is executed NOT in main thread then the error happened.Rick Clephas
03/04/2022, 6:26 AM