Is there a way to expose an `interface` from Kotli...
# multiplatform
Is there a way to expose an
from Kotlin which declares `suspend fun`s (or some other async construct), and have an iOS app app directly implement the interface in Swift, rather than implementing it through KMP? Our mobile data layers are currently platform-specific and have a ton of moving parts. It's definitely not a short-term project to convert them to KMP (and we're not sure if we would ever really want to). We do however want to migrate more logic into KMP! We were hoping there's a way to: • define KMP interfaces for Use Cases/Interactors, • implement those interfaces using our (non-KMP) data layers in iOS/Android, and then • constructor-inject those Use Case implementations into a KMP ViewModel, using them from common code Anyone have any insight?
👀 5
What you wish is possible and it’s exactly what I am doing. For example, I have an
class (KMP) that receive as a construct an instance of an
interface that is implemented in iOS and Android. A “foreseable” downside, the constructor-injected must be done within each platform.
❤️ 1
What does your interface look like? Are you defining something like
suspend fun getUser(id: String): User
in it, or are you limited to just
-returning blocks that log analytic events given a payload?
I don’t use suspend fun in there. I only use blocks, Runnable
Suspend funs on swift just get an extra argument - the callback
so if you do that ^ the suspend function is executed in the main scope by default?
Depends on the caller.
mm but from swift you cannot declare a scope right?
If you call via the automatic callback adapter, I think it's main dispatcher, yes. If you call it from Kotlin, it's whatever the current CoroutineContext has (which depends on the parent scope)
yup of course, I was wondering the swift route 👍
But the callback is used to define a suspend fun in KMP and run it in Swift, right? I’m specifically looking for the opposite: a mechanism to define asynchronous code in Swift and run it in KMP
@ankushg I think you could make a Kotlin adapter that can take a continuation and callbacks to bridge to that continuation, and use it in Swift overrides.