-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add Support for Kotlin/Native #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@ScottPierce It's written Kotlin/Native ;-) |
That is something we'll be working on pretty soon. Stay tuned. |
Awesome! I assume that #201 would also be resolved with that work as well? I found the lack of channels in the javascript side of this library fairly crippling, and it would be the same for kotlin-native. |
For the time being, brining basic coroutines (launch/async) to Kotlin/Native has more priority than bringing the channels everywhere. But it will be the next step. I expect that we'll be able to bring channels to both JS and Native in one swoop as soon as native basics are established. |
Is there any update on this? I've been working on an Android app, and splitting up code into common modules with the hopes that I can share code with ios via Kotlin/native. This is currently the only issue that I know of that's really stopping me from using app code and compiling our codebase for iOS. |
Work was progressing on this in this branch, but seems to have stagnated: https://github.com/Kotlin/kotlinx.coroutines/tree/mpp |
We are interested in this too. Any updates? |
@ScottPierce If you are really starving to use coroutines in multi-platform kotlin project for Android and iOS you could take a look at demo project from mobius conference talk Multiplatform architecture with Kotlin for iOS and Android. Here is video in russian and presentation file in english Code in common module looks like: override suspend fun getTickersForAllMarkets(): List<Pair<Market, Ticker>> {
return async(context) { repository.getAllMarkets() }
.await()
.subList(0, 20)
.map { market ->
market to async(context) {
repository.getTicker(market)
}
}
.map { (market, job) ->
market to job.await()
}
} To implement coroutines for iOS they declared the I had EXC_BAD_ACCESS issues with AsyncDispatcher implementation on iOS. After replacement of |
@kengura Thanks for the recommendation. I'll give that a shot with my team. |
@kengura @ScottPierce I took a look at the demo implementation. It's very interesting. I'd love to have coroutines on iOS, but it's tricky. I'm guessing the reason kotlinx.coroutines is taking a while to materialize on native is due to state and threading rules. I wouldn't put the worker-based solution into anything critical. Unless I really misunderstand the domain and how coroutines are routing, the worker implementation will eventually fail (or have memory leaks).
'value' and 'continuation' aren't frozen. If you used 'TransferMode.CHECKED' I assume it would blow up and tell you that. The issue you're going to have is reference counting in K/N is not atomic on regular data, but is atomic on frozen data. You'll eventually wind up with the same crash that you saw with the dispatch async example. I got that error, and in my case, it was trying to free memory that's already been freed. That's probably because its trying to move non-frozen data around to different threads. In the worker example, at some point, dispatchResume will try to return while schedule block is starting, or some combination thereof, and you'll either have leaks or crashes for the same reason. I think, anyway. I'd try hammering those calls hard in a test to see if it'll crash before putting the code in anything you'd ship. As a general rule, if TransferMode.CHECKED is failing, you could be in trouble. |
@kpgalligan Yes you are right. This problem stops me from using such solution anywhere except test project. But someone could find better approach. |
I've noticed that Kotlin/native's threading model makes quite a few things more difficult. I wonder if raw shared memory could be used as a solution. |
Late update: we're working on this.
|
@qwwdfsad The latest kotlin/native blog post says:
I'm not seeing anything like this for kotlinx.couroutines yet. Can you point me in the right direction? |
We are still working on proper publishing. There is a test version |
I am trying to use Are you able to point me to to an example showing how to correctly set up these coroutines dependencies in a Konan project? |
You'll need the recent version of Gradle and you should use new DSL for dependencies declarations like here: https://github.com/Kotlin/kotlinx.coroutines/blob/native/gradle/compile-native.gradle and your Kotlin/Native version shall exactly match the one that was used to build that library: https://github.com/Kotlin/kotlinx.coroutines/blob/native/gradle.properties |
Will it be available for Windows/Linux/Android/Wasm soon? |
Hi @elizarov, I followed your instructions, but unfortunately it doesn't seem to change anything. As I don't wish to pollute this thread, perhaps you could help me over here: Thanks :) |
@brettwillis It is coming. You can check the docs on using in Kotlin/Native project here (the compatible build is not released yet, but you can already build it yourself from |
@AregevDev Windows/Linux/iOS/MacOS/Android is coming, but no Wasm support is planned in the short term. |
@elizarov I am able to use coroutines in a kotlin native program using the new DSL. But I see that the new plugin does not support setting the output kind to generate a dynamic library (.so/.dylib/*.dll). Since the new plugin isn't an option a the moment, is there anyway to use coroutines with the 'konan' plugin so I can generate a dynamic library? adding:
with apply plugin: 'konan' causes
|
@luca992 |
@r4zzz4k Yes I understand, but I am not planning on interacting with the coroutines in C. I plan on calling a kotlin function from C. Have kotlin perform an http request using curl. Then once it gets a result, pass the data to C through an interface. I'm not sure if I'm being clear.... but basically, coroutines will only be used internally and I just need to be able to call some kotlin functions from C code |
@luca992 okay, now I re-read your message and found JetBrains/kotlin-native#1877, I see that you are asking for |
@r4zzz4k I do not have access to a .klib for kotlix.coroutines. Are you saying I could clone this project and build a klib for my to project use via konanArtifacts? |
@luca992 I didn't really try to use native version of |
The You should be able to produce a |
@elizarov |
@r4zzz4k your suggestion to use the klib |
You are right. You'll need additional compiler options with
|
@elizarov That works too. Thanks for the help |
Hi @elizarov , I used kotlinx.coroutines for my shared library project, with the include as Where version was The common code co-routines work fine from Android. This produced a .framework successfully for use in iOS. However, all the How can this be used in iOS? Or do we need to have an async wrapper in the common code as well strictly for iOS usage? |
Hi @steve-the-edwards, Kotlin is the only programming language that supports suspending functions for now, so you need to write callback wrappers if you want to call those from other programming languages like Swift or Objective-C. This code can be in an iOS specific source set, no need to pollute the common code with this. |
Thanks @LouisCAD yes that makes sense. I wondered if these wrappers had been done in a generic way as part of the kotlinx.coroutines-native library. |
Now that Kotlin-native supports multi-platform projects, this library can add support for Kotlin-native.
The text was updated successfully, but these errors were encountered: