Is there a way to expose an `interface` from Kotli...
# multiplatform
a
Is there a way to expose an
interface
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
s
What you wish is possible and it’s exactly what I am doing. For example, I have an
Analytics
class (KMP) that receive as a construct an instance of an
AnalyticsExecutor
interface that is implemented in iOS and Android. A “foreseable” downside, the constructor-injected must be done within each platform.
❤️ 1
a
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
Unit
-returning blocks that log analytic events given a payload?
s
I don’t use suspend fun in there. I only use blocks, Runnable
b
Suspend funs on swift just get an extra argument - the callback
f
so if you do that ^ the suspend function is executed in the main scope by default?
l
Depends on the caller.
f
mm but from swift you cannot declare a scope right?
l
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)
f
yup of course, I was wondering the swift route 👍
a
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
l
@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.