Nikolay Kasyanov

08/31/2021, 3:02 PM
👋 hey folks Has anyone tried to expose an interface with a suspend function and implementing this interface in Swift? Is it even expected to work? I’m trying this rn and if I call
not on the same thread the “coroutine” method was called on, I get illegal sharing exception:
class DependencyImpl: FooBarDependency {
    func getName(completionHandler: @escaping (String?, Error?) -> Void) {
When thread stays the same, it’s all good. I’m using coroutines 1.5.1-native-mt. Kotlin code in comments.
I’m guessing coroutine continuations cannot be shared, hence the exception?
class FooBar(val dependency: Dependency) {
    interface Dependency {
        suspend fun getName(): String

    fun doStuff(callback: (String) -> Unit): Disposable {
        val suspendCall: suspend () -> String = dependency::getName

        return suspendCall
            .map { "Hello $it" }
            .subscribe {


08/31/2021, 3:35 PM
The built-in suspend function translation is expected to only work single-threaded (and maybe only main-threaded, I don't remember for sure). It's limited by the fact that it exists at the language level and so can't make use of constructs from the kotlinx.coroutines library. Might be interesting to see what happens in the new memory model preview though. I haven't looked at that yet.

Nikolay Kasyanov

09/01/2021, 7:38 AM
Thanks for chiming in @russhwolf, sticking to single thread for now.

Alexander Ignatiev

09/01/2021, 8:17 AM
hi I had a similar problem it helped me
DispatchQueue.main.async {
:thank-you: 1

Nikolay Kasyanov

09/02/2021, 8:04 AM
while we’re at it, is there a way to react to coroutine cancellation on the iOS side, if the only thing we have is a callback?