Thread
#multiplatform
    ankushg

    ankushg

    1 year ago
    Is there a way to expose an
    interface
    from Kotlin which declares suspend funs (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?
    s

    Sebastien Leclerc Lavallee

    1 year ago
    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.
    ankushg

    ankushg

    1 year ago
    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

    Sebastien Leclerc Lavallee

    1 year ago
    I don’t use suspend fun in there. I only use blocks, Runnable
    Big Chungus

    Big Chungus

    1 year ago
    Suspend funs on swift just get an extra argument - the callback
    Ferran

    Ferran

    1 year ago
    so if you do that ^ the suspend function is executed in the main scope by default?
    louiscad

    louiscad

    1 year ago
    Depends on the caller.
    Ferran

    Ferran

    1 year ago
    mm but from swift you cannot declare a scope right?
    louiscad

    louiscad

    1 year ago
    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)
    Ferran

    Ferran

    1 year ago
    yup of course, I was wondering the swift route 👍
    ankushg

    ankushg

    1 year ago
    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
    louiscad

    louiscad

    1 year ago
    @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.