I think part of it is that K/N doesn’t support cor...
# kotlin-native
e
I think part of it is that K/N doesn’t support coroutines off the main thread - does anyone know if there’s some place where an explanation of why is posted?
It's part of a longer discussion of the changes in K/N around concurrency. Basically, state and threading are different. There are more runtime controls. It is a work in progress, but from what I can gather the team has focused on getting to something like a 1.0 for coroutines in JVM, and other platforms should be coming in the next few months. Nobody has said anything publicly about dates, but my wildly uninformed best guess is Q1 next year. Hopefully January.
e
Yeah - I went to the KC session on
freeze
etc
it’s just a bit frustrating that ktor requires coroutines but support for it is kinda half baked
k
Yeah, definitely some tough bits around multiplatform right now, but that's how these things go. I'm adding ktor to our samples now. We're using more "standard" concurrency with workers currently.
r
The thing to remember is that for Ktor purposes, there's not much need for other threads since you're just waiting on a web response. Situation is very different when you're doing computational or db work where you actually need to offload stuff to a background thread rather than just suspending while you wait for something to happen externally.
k
I've rewritten our sqlite driver (from the ground up, because I have issues). Have been trying to get creative with coroutines and the underlying sqlite library, but it's difficult. Essentially trying to suspend the caller while sqlite runs, but not all operations are universal and/or slow. Long story.
As far as I can tell, ktor's client delegates to ios's URLSession and async callbacks, so in theory main thread networking calls should perform pretty good (unless you're doing large calls and parsing, but still). I didn't spend a ton of time picking apart ktor's lib code, though.
r
Right, that's my assumption without having dug into the code. For wrapping callback-based APIs like web clients tend to be, single-threaded coroutines seem pretty straightforward, But for your SQL stuff I imagine it's a lot harder.
k
For sql, you can definitely make large course-grained calls suspend easily, but the smaller calls would need to have very performant async apis. For "fun" I tried this with a worker doing the actual backgrounding and it's bad. The issue being most low level sql calls are super fast, but occasionally they're really not. The call to get the next row is 'sqlite3_step', which can be fully in memory cache, or execute a complex query on the db, and you don't know beforehand. Anyway, yeah. Long story. I'm giving a talk on "Kotlin Multiplatform in Production" in a few weeks. Everybody seems to want to use coroutines assuming a multithreaded future, but you really can't avoid hand-rolling concurrency today on some level for a production app. Wish I was giving this talk in Feb/March 🙂
e
Yeah on iOS it’s definitely running asynchronously - it’s not blocking the run loop at all so things print when the requests come in
k
The only concern there would be if you were loading a large chunk of data and potentially parsing/storing it without some sort of yield in the operation. Essentially, just need to be careful on data loads. I did our sample before ktor and the kotlinconf app were really available and just did a basic 'get' on a background tasks. Moving to ktor will be a little weird as the parse and store operations will dip in the db and should probably be in the background, but then again, it's just on app start and is an iOS app for an Android conference, so you know. Probably not doing any "business" harm if we skip some frames: https://github.com/touchlab/DroidconKotlin/blob/master/sessionize/lib/src/iosMain/kotlin/co/touchlab/sessionize/platform/Functions.kt#L90
r
@kpgalligan I don't think they've announced the date yet but DroidCon Boston tends to be March/April if you're looking for somewhere else to talk about this stuff in a few months 🙂
k
Will probably submit. I'm trying to break out of just Android conferences, though. Speaking at devfest florida in Jan, although that had a lot to do with being able to visit disneyland/world. Speaking at the iOS meetup in NYC in Jan as well. See how that goes...