I'm trying out the new 1.4 capability of being abl...
# multiplatform
j
I'm trying out the new 1.4 capability of being able to call
suspend
functions (that use Ktor) directly from iOS client but running in to a few issues (thread)...
this is example of function I'm trying to call (currently called by Android client)
Copy code
suspend fun fetchNetworkList() : Map<String, List<Network>> {
    val result = cityBikesApi.fetchNetworkList()
    return result.networks.groupBy { it.location.country }
}
(
cityBikesApi.fetchNetworkList()
uses Ktor to make api request)
And up to now have been calling following from iOS
Copy code
fun fetchNetworkList(success: (Map<String, List<Network>>) -> Unit) {
    ktorScope  {
        success(fetchNetworkList())
    }
}
(
ktorScope
maps to
GlobalScope.launch(Dispatchers.Main)
on iOS but
runBlocking
on macOS to workaround issue currently with using
native-mt
version of kotlinx coroutines on macoS)
if call suspend function directly from iOS I'm getting following errors
Copy code
<https://api.citybik.es/v2/networks/galway> failed with exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.call.HttpClientCall@30bde68
but if I add following
withContext
to method then it works for some reason
Copy code
suspend fun fetchNetworkList() : Map<String, List<Network>> {
    return withContext(Dispatchers.Main) {
        val result = cityBikesApi.fetchNetworkList()
        result.networks.groupBy { it.location.country }
    }
}
In either case I am calling the
suspend
function only from main thread (as mentioned in https://blog.jetbrains.com/kotlin/2020/06/kotlin-1-4-m2-released/#native-suspending-functions-support) . @russhwolf this was the issue I mentioned on twitter....just in case you've come across this.
r
Hmm. I should stop guessing it's the main-thread thing because I've been wrong every time
Not sure what else, though. Probably a ktor thing like Kevin was suggesting on Twitter but I don't have any better insight
j
Yeah, the interworking of ktor and native-mt version of kotlinx coroutines is somewhat of an unknown right now....perhaps have been lucky that it's worked in so far as it has up to now
r
You could try building a similar structure without native-mt coroutines and see if that still causes trouble
j
what's strange is that it works with that
withContext(Dispatchers.Main)
....given that it should have been invoked on main thread anyway.....following article mentions some unknowns about what scope/dispatcher for example are being used https://dev.to/touchlab/kotlin-1-4-suspend-functions-209
k
ktor doesn't support native-mt. there are certain conditions under which it happens to work, but it's quite brittle.
it has to do with whether certain ktor internal state gets frozen or not by the runtime
j
yeah, I think I'm going to have to realistically wait until that's sorted before progressing any further with this
r
Yeah I'm hoping we get news there on the ktor front soon but haven't seen/heard anything yet. If it's becoming an issue you might be better off returning to standard coroutines for the time being
We don't have all the KaMPKit dependencies ready for 1.4 yet, but once we do I'd like to try updating that project and see if we hit something similar.
j
Can probably do that in this particular repo but in others I'm using SQLDelight which I believe depends on use of
native-mt
version
at least I think it does for flow support that was added a little while back
r
Yes it does. If you don't include that module I think you'd be ok
k
this is a primary reason I dropped ktor
you can create your own HttpClient class (at least for Android and iOS) in a matter of an hour or two
vs. spending days trying to get ktor working
1
k
We’re not using ktor on client work until that’s resolved. To be precise, you can use mt coroutines with ktor, as long as the Job that’s part of the scope you’re in doesn’t get frozen. Effectively that means you either have a separate ktor scope, or do something complicated like we did with KaMP Kit. Neither is fun. Star this issue: https://youtrack.jetbrains.com/issue/KTOR-499