Matheus Finatti
11/11/2021, 3:30 PMsuspend
function in Swift
I have an interface, e.g.:
interface Loader {
suspend fun fetch()
}
which I implement in Android and in iOS.
The iOS version looks like this:
public class RemoteFeatureLoader : Loader {
public func fetch(completionHandler: @escaping (KotlinUnit?, Error?) -> Void) {
doAsyncStuff { [weak self] in
completionHandler(KotlinUnit(), nil)
}
}
}
And the caller in the shared code looks like this
class CommonLoader() {
override suspend fun fetch() {
localFeatureLoader.fetch()
remoteFeatureLoader.fetch()
}
}
and finally, the CommonLoader.fetch()
is being called from a view model which is also shared
class CommonViewModel(
private val scope: CoroutineScope,
private val uiContext: CoroutineContext,
) : ViewModel {
override fun startup() {
scope.launch(context = uiContext) {
loader.fetch()
}
}
}
Basically, the Android version works like a charm, but the iOS version hangs at loader.fetch()
If I add a log after remoteFeatureLoader.fetch()
it is printed just fine, but the function never returns/resumes from its suspended state.
Does anyone have any clue what could be going on? I appreciate any help! Thank you!ankushg
11/11/2021, 3:55 PMinterface
intended to be implemented outside of Kotlin with a callback-based API. This lets us pretty easily implement them in Swift.
We then convert the callbacks to `suspend fun`s within our KMP codebase, so we can use coroutines within Kotlin.Matheus Finatti
11/11/2021, 4:02 PMpublic func fetch() async throws -> KotlinUnit
and
public func fetch(completionHandler: @escaping (KotlinUnit?, Error?) -> Void)
I couldn’t try the first one as it is only for iOS 15 at the moment, but the second one should theoretically workankushg
11/11/2021, 4:44 PMsuspend fun
-> completionHandler
mapping is only be unidirectional (Kotlin -> Swift) and may not be supported in the opposite directionmittapalli hareesh
03/08/2022, 5:48 AM