Regarding support for calling suspend functions fr...
# multiplatform
j
Regarding support for calling suspend functions from Swift/Objective C.....I think this was asked here before but don't remember seeing an answer.....is anyone aware of how this is implemented under the good, and what is setup for example regarding dispatcher used and cancellations? I think many projects are still rolling their own approach to this primarily so they have control for example of lifecycle aware cancellation.
s
I’m aware, feel free to ask any questions. The implementation doesn’t dispatch anything additionally, it relies on suspend function itself to take care of threads (as it happens often with K/N at the moment; see e.g. native-mt kotlinx.coroutines). In fact empty coroutine context is used: https://github.com/JetBrains/kotlin-native/blob/03af05e9fc396b14b2d2758ab20619529f93ecf6/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt#L30 So when Swift calls suspend function K/N runtime creates a coroutine that is not cancellable. The only workaround to make it cancellable I’m aware of is: define a wrapping
suspend
function in Kotlin. Please consider creating a feature request: https://kotl.in/issue
👍 1
j
@svyatoslav.scherbina I'm curious if you've got much feedback about not being able to cancel? I guess typically on Android side we'd use lifecycle aware
viewModelScope
(if using Android AAC
ViewModel
).....so ideally what would be used in Swift would provide something similar
s
Well, not yet 🙂 But I guess not so many people have already migrated to 1.4 and are starting adoption of the feature. This use case is known; if there are enough user demand, we would consider supporting it.
j
fwiw some related discussion in following thread https://twitter.com/arkann1985/status/1297216439565393920?s=19
🙏 1
cc @kpgalligan