Marc Knaup
06/12/2019, 12:26 AMHttpClient
in a Worker
(background thread) on native 🙂TransferMode.UNSAFE
to move(!) the HttpClient
instance to your Worker
. Unless you create it directly within the worker in which case this is not necessary. In both cases it is stored in an @ThreadLocal
variable.
worker.execute(TransferMode.UNSAFE, { HttpClient(Ios) }) { httpClient -> threadLocalHttpClient = httpClient }
HttpClient
calls inside your Worker
.
GlobalScope.launch(Dispatchers.Unconfined) {
// use HttpClient
}
body = TextContent(
text = someJson,
contentType = ContentType.Application.Json
)
->
body = ByteArrayContent(
bytes = someJson.toUtf8(),
contentType = ContentType.Application.Json
)
HttpClient
instance on a non-main thread until this is fixed:
https://github.com/ktorio/ktor/issues/1183
There may be more of such issue though.NSURLSession
responds on the main thread and the coroutine continues on that thread due to Unconfined
😞
Is there a better way of getting the coroutine execution back into the Worker
rather than using worker.execute(TransferMode.UNSAFE, …)
?e5l
06/13/2019, 12:41 PMatomicfu
primitives and it can't be solved on ktor side for now.