https://kotlinlang.org logo
z

zsperske

07/15/2020, 3:17 PM
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.
Copy code
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

07/15/2020, 3:24 PM
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
k

Kris Wong

07/15/2020, 3:29 PM
in Kotlin 1.4 this will create a callback based binding for you
😍 2
in 1.3 you can add one yourself
z

zsperske

07/15/2020, 3:30 PM
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
k

Kris Wong

07/15/2020, 3:53 PM
suspend functions are removed in 1.3
👍 1
b

basher

07/15/2020, 3:53 PM
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

07/15/2020, 3:54 PM
Cool! Thanks again
am I going down the right path here?
Copy code
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")
    }
3 Views