z

    zsperske

    2 years ago
    Hello, I have a pretty basic question about iOS in a KMP project. Say I have an interface defined in my common code that looks like this.
    interface Database {
         suspend fun updateEntry(foo: Foo) : Boolean
    }
    In order to implement this on iOS one option appears to be implementing a kotlin class in my iosMain that could use obj-c interop to achieve what I want. Is it also possible to feed a swift class that I've written (with obj-c headers) into my common code? Are there drawbacks to that?
    b

    basher

    2 years ago
    you can feed a Swift class to Kotlin. Obj-C headers aren't needed. Swift compiler will let you know that to implement the interface, which is exported as an Obj-C protocol, you'll need to make your Swift class inherit from NSObject
    Kris Wong

    Kris Wong

    2 years ago
    in Kotlin 1.4 this will create a callback based binding for you
    in 1.3 you can add one yourself
    z

    zsperske

    2 years ago
    Thank you both for the replies, do you know of any examples of this on github or in the docs?
    It seems I'm running into the same issue as this guy, the protocol being generated for me is empty. I'm gonna check out the suggestions that he was given though
    Kris Wong

    Kris Wong

    2 years ago
    suspend functions are removed in 1.3
    b

    basher

    2 years ago
    oh i missed the suspend bit of this. for this on iOS, we make a callback-based fun, which is compatible in 1.3. then we make an internal suspend extension that calls the callback version using this https://github.com/autodesk/coroutineworker#waiting-on-asynchronous-callback-based-work
    z

    zsperske

    2 years ago
    Cool! Thanks again
    am I going down the right path here?
    override suspend fun addEvent(host: User, event: Event): Boolean {
            return suspendCoroutine {
                addEvent(it)
            }
        }
    
        open fun addEvent(callback: Continuation<Boolean>) {
            throw NotImplementedError("iOS project should implement this")
        }