John O'Reilly
08/12/2020, 6:08 PMsuspend
functions (that use Ktor) directly from iOS client but running in to a few issues (thread)...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)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)<https://api.citybik.es/v2/networks/galway> failed with exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen io.ktor.client.call.HttpClientCall@30bde68
withContext
to method then it works for some reason
suspend fun fetchNetworkList() : Map<String, List<Network>> {
return withContext(Dispatchers.Main) {
val result = cityBikesApi.fetchNetworkList()
result.networks.groupBy { it.location.country }
}
}
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.russhwolf
08/12/2020, 6:39 PMJohn O'Reilly
08/12/2020, 6:40 PMrusshwolf
08/12/2020, 6:40 PMJohn O'Reilly
08/12/2020, 6:42 PMwithContext(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-209Kris Wong
08/12/2020, 6:46 PMJohn O'Reilly
08/12/2020, 6:49 PMrusshwolf
08/12/2020, 6:51 PMJohn O'Reilly
08/12/2020, 6:52 PMnative-mt
versionrusshwolf
08/12/2020, 6:53 PMKris Wong
08/12/2020, 7:34 PMkpgalligan
08/13/2020, 9:05 PM