:wave: hey folks Has anyone tried to expose an in...
# ios
n
👋 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
completionHandler
not on the same thread the “coroutine” method was called on, I get illegal sharing exception:
Copy code
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?
Copy code
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
            .asSingle()
            .subscribeOn(ioScheduler)
            .observeOn(mainScheduler)
            .map { "Hello $it" }
            .subscribe {
                callback(it)
            }
    }
}
r
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.
n
Thanks for chiming in @russhwolf, sticking to single thread for now.
a
hi I had a similar problem it helped me
Copy code
DispatchQueue.main.async {
    completionHandler()
}
🙏 1