gonzooin
03/11/2019, 2:28 PMkotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen
error when i'm using on iOS ( the android part work well BTW )
I've defined a specific dispatcher for iOS like this :
internal val ApplicationDispatcher: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue())
internal class NsQueueDispatcher(
private val dispatchQueue: dispatch_queue_t
) : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatch_async(dispatchQueue) {
block.run()
}
}
}
And I use like this in a presenter that I will connect to the iOS UIViewController
class ExampleViewPresenter : BasePresenter() {
fun test() {
launch {
try {
val example = ExampleSuspendMethod()
withContext(Main) {
println(example)
}
} catch (e: Throwable) {
println(e)
}
}
}
I've defined the scope of launch
in BasePresenter like this:
abstract class BasePresenter : CoroutineScope {
private var job = Job()
override val coroutineContext: CoroutineContext
get() = ApplicationDispatcher + job
fun onDestroy() {
job.cancel()
}
}
I've found people with the same error on stack overflow and on this channel but the cause was a bug in Kotlin 1.3.11
and I'm using 1.3.21
.
I've found one similar project with the same version of Kotlin but I didn't succeed to make it work ( https://github.com/watabee/RakutenRankingKotlin )
Did anyone succeed to use Ktor client on iOS with Kotlin 1.3.21
?e5l
03/11/2019, 2:38 PMMain
dispatcher in your code?gonzooin
03/11/2019, 2:53 PMMain
is not useful, old test code…Ellen Shapiro
03/11/2019, 8:19 PMpost
request - I tried pulling out the HttpRequestBuilder
to a var instead of using a lambda, and it managed to get to the actual request before dyingval fullPath = fullURLStringForPath(path)
var builder = HttpRequestBuilder()
builder.url(fullPath)
builder.body = data
headers.forEach { builder.header(it.key, it.value) }
println("POSTUN $data")
return <http://ktorClient.post|ktorClient.post>(builder)
println
before it throws the exception<http://HttpClient.post|HttpClient.post>
down to HttpClient.request(Builder)
to call.builder
, I found this in io.ktor.client.call
- the comments certainly say it mutates.io.ktor.client.call.utils.kt
- it’s in io.ktor.client.request.HttpRequest.kt
post
/ get
etc methods goes through this which calls that:gonzooin
03/12/2019, 11:10 AMobject
) ?Ellen Shapiro
03/12/2019, 11:18 AMribesg
03/12/2019, 11:23 AMEllen Shapiro
03/12/2019, 11:32 AMribesg
03/12/2019, 11:44 AMHttpClient
instance inside an object
Ellen Shapiro
03/13/2019, 6:06 PMobject
, so thank yougonzooin
03/13/2019, 6:25 PMe5l
03/13/2019, 7:15 PM