https://kotlinlang.org logo
#ktor
Title
# ktor
a

Aleksey Chugaev

12/18/2019, 9:16 PM
Hi, I am using ktor client on iOS and trying to move API requests to background thread, but get this error:
Copy code
Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread
        kfun:kotlin.Throwable.<init>(kotlin.String?)kotlin.Throwable + 87
        kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85
        kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85
        kfun:kotlin.native.IncorrectDereferenceException.<init>(kotlin.String)kotlin.native.IncorrectDereferenceException + 85
        ThrowIncorrectDereferenceException + 137
        CheckIsMainThread + 25
        kfun:io.ktor.http.<get-URL_ALPHABET_CHARS>#internal + 35
        kfun:io.ktor.http.encodeURLPath@kotlin.String.()kotlin.String + 717
        kfun:io.ktor.http.takeFromUnsafe$ktor-http@io.ktor.http.URLBuilder.(kotlin.String)io.ktor.http.URLBuilder + 2278
        kfun:io.ktor.http.takeFrom@io.ktor.http.URLBuilder.(kotlin.String)io.ktor.http.URLBuilder + 263
My code is
Copy code
...
withContext(workerDispatcher){
    val loginResponse = <http://client.post|client.post><LoginNetworkResponse>("$apiEndpoint/init/auth?v=1") {
            contentType(ContentType.Application.Json)
            body = LoginNetworkRequest(
                ...
            )
        }
   ....
}
c

cy

12/18/2019, 9:24 PM
This is bug. No workaround.
a

Aleksey Chugaev

12/18/2019, 9:39 PM
Has it been raised anywhere for tracking?
c

cy

12/18/2019, 9:41 PM
Yes, we aware of it: we have tons of top-level vals that don’t work with K/N. Need to cleanup them somehow.
a

Aleksey Chugaev

12/18/2019, 9:43 PM
Do you have a target release or date for it yet?
c

cy

12/18/2019, 10:01 PM
I hope 1.3.0
👍 1
1.3.0-rc still doesn’t work
a

Aleksey Chugaev

12/18/2019, 10:06 PM
No it doesn't, I updated to RC today