kotlin-coroutines

How to use code that relies on ThreadLocal with Kotlin coroutines

北城以北 提交于 2019-12-03 06:57:25
问题 Some JVM frameworks use ThreadLocal to store the call context of a application, like the SLF4j MDC, transaction managers, security managers, and others. However, Kotlin coroutines are dispatched on different threads, so how it can be made to work? (The question is inspired by GitHub issue) 回答1: Coroutine's analog to ThreadLocal is CoroutineContext. To interoperate with ThreadLocal -using libraries you need to implement a custom ContinuationInterceptor that supports framework-specific thread

Existing 3-function callback to Kotlin Coroutines

丶灬走出姿态 提交于 2019-12-03 06:53:31
问题 I have a general question with a specific example: I'd like to use Kotlin coroutine magic instead of callback hell in Android when taking a picture. manager.openCamera(cameraId, object : CameraDevice.StateCallback() { override fun onOpened(openedCameraDevice: CameraDevice) { println("Camera onOpened") // even more callbacks with openedCameraDevice.createCaptureRequest().... } override fun onDisconnected(cameraDevice: CameraDevice) { println("Camera onDisconnected") cameraDevice.close() } ...

How to use code that relies on ThreadLocal with Kotlin coroutines

☆樱花仙子☆ 提交于 2019-12-02 20:36:24
Some JVM frameworks use ThreadLocal to store the call context of a application, like the SLF4j MDC , transaction managers, security managers, and others. However, Kotlin coroutines are dispatched on different threads, so how it can be made to work? (The question is inspired by GitHub issue ) Coroutine's analog to ThreadLocal is CoroutineContext . To interoperate with ThreadLocal -using libraries you need to implement a custom ContinuationInterceptor that supports framework-specific thread-locals. Here is an example. Let us assume that we use some framework that relies on a specific ThreadLocal

How to download PDF file with Retrofit and Kotlin coroutines?

痞子三分冷 提交于 2019-12-02 17:02:07
问题 I saw topics like How to download file in Android using Retrofit library?, they use @Streaming and RxJava / callbacks. I have Kotlin, coroutines, Retrofit 2.6.0 and queries like in https://stackoverflow.com/a/56473934/2914140: @FormUrlEncoded @Streaming @POST("export-pdf/") suspend fun exportPdf( @Field("token") token: String ): ExportResponse I have a Retrofit client: retrofit = Retrofit.Builder() .baseUrl(SERVER_URL) .client(okHttpClient) .build() service = retrofit.create(Api::class.java)

How do Kotlin coroutines work internally?

☆樱花仙子☆ 提交于 2019-12-02 16:49:51
How does Kotlin implement coroutines internally? Coroutines are said to be a "lighter version" of threads, and I understand that they use threads internally to execute coroutines. What happens when I start a coroutine using any of the builder functions? This is my understanding of running this code: GlobalScope.launch { <---- (A) val y = loadData() <---- (B) // suspend fun loadData() println(y) <---- (C) delay(1000) <---- (D) println("completed") <---- (E) } Kotlin has a pre-defined ThreadPool at the beginning. At (A) , Kotlin starts executing the coroutine in the next available free thread

how to break from lambda passed to recursive function when provided condition met

我的梦境 提交于 2019-12-02 08:32:58
I am writing a custom loop dsl and I want it's usage to look like below var counter1 = 0 var counter2 = 0 loop { counter1 += 1 println(counter1) stopIf(counter1 == 5) // loop should terminate here and not execute rest of the code if condition matches counter2 += 2 println(counter2) stopIf(counter2 == 8) // loop should terminate here and not execute rest of the code if condition matches } I have following code which does allows me to write stopIf any number of times and anywhere in the loop body but when condition matches it does not terminate immediately but executes rest of the loop body and

How to download PDF file with Retrofit and Kotlin coroutines?

可紊 提交于 2019-12-02 08:30:02
I saw topics like How to download file in Android using Retrofit library? , they use @Streaming and RxJava / callbacks. I have Kotlin, coroutines, Retrofit 2.6.0 and queries like in https://stackoverflow.com/a/56473934/2914140 : @FormUrlEncoded @Streaming @POST("export-pdf/") suspend fun exportPdf( @Field("token") token: String ): ExportResponse I have a Retrofit client: retrofit = Retrofit.Builder() .baseUrl(SERVER_URL) .client(okHttpClient) .build() service = retrofit.create(Api::class.java) If a token parameter is right, the query returns PDF file: %PDF-1.4 %���� ... If it is wrong, it will

Implementing coroutines in Java

烂漫一生 提交于 2019-11-30 06:11:09
问题 This question is related to my question on existing coroutine implementations in Java. If, as I suspect, it turns out that there is no full implementation of coroutines currently available in Java, what would be required to implement them? As I said in that question, I know about the following: You can implement "coroutines" as threads/thread pools behind the scenes. You can do tricksy things with JVM bytecode behind the scenes to make coroutines possible. The so-called "Da Vinci Machine" JVM

How can I send items to a Kotlin.Flow (like a Behaviorsubject)

爷,独闯天下 提交于 2019-11-29 20:10:29
问题 I wanted to know how can I send/emit items to a Kotlin.Flow , so my use case is: In the consumer/ViewModel/Presenter I can subscribe with the collect function: fun observe() { coroutineScope.launch { // 1. Send event reopsitory.observe().collect { println(it) } } } But the issue is in the Repository side, with RxJava we could use a Behaviorsubject expose it as an Observable/Flowable and emit new items like this: behaviourSubject.onNext(true) But whenever I build a new flow: flow { } I can

Kotlin: withContext() vs Async-await

吃可爱长大的小学妹 提交于 2019-11-29 19:56:44
I have been kotlin docs and if I understood correctly the two kotlin functions work as follows : withContext(context) : switches the context of the current coroutine, when the given block executes, the coroutine switches back to previous context. async(context) : Starts a new coroutine in the given context and if we call .await() on the returned Deferred task, it will suspends the calling coroutine and resume when the block executing inside the spawned coroutine returns. Now for the following two versions of code : Version1: launch(){ block1() val returned = async(context){ block2() }.await()