Hi, I have a question about implementing Kotlin suspend functions in Swift with an Kotlin interface.
AFAIK, suspend functions are translated as functions with completion handler in the generated ObjC header. In Swift, you then have the option of implementing the functions either with a completion handler or as an async/await function.
If I’m using async/await as follows, is it safe? Does the @MainActor annotation work correctly? Is cancellation supported?
// Kotlin
interface LoginProvider {
suspend fun login()
}
// Kotlin
class AndroidLoginProvider: LoginProvider {
override suspend fun login() { }
}
// Swift
class IOSLoginProvider: LoginProvider {
@MainActor
func login() async { }
}
// Kotlin
class AuthenticationRepository {
[...]
fun login() = withContext(Dispatchers.IO) {
loginProvider.login()
}
}
In the KMP documentation, I only find a clear indication that async/await usage is experimental when suspend functions are called from swift (see
https://kotlinlang.org/docs/native-objc-interop.html#suspending-functions). However, this is not the case when a suspend function is implemented via an interface.
Also this article
https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-connect-to-apis.html describes only how to implement such apis in the iOS source set in shared.
If anyone can provide me with further information or sources, I would be very grateful. Thank you :)