kotlinx.coroutines

Kotlin coroutines handle error and implementation

好久不见. 提交于 2020-06-24 07:35:36
问题 Using coroutines for the first time. Need help. Here is my flow: Presenter wants to login so calls Repository Interface. Repository implements RepositoryInterface. So Repository calls APIInterface. APIInterface is implemented by APIInterfaceImpl. The APIInterfaceImpl finally calls the MyRetrofitInterface. Here is the flow diagrammatically: Presenter -> Repository -> APIInterfaceImpl -> MyRetrofitInterface Once I get login response: APIInterfaceImpl -> Repository -> Stores the data in cache ->

“+” in Kotlin Coroutines?

我的梦境 提交于 2020-05-08 04:11:10
问题 This is example code for a Cancellation via explicit job for Kotlin Coroutines: fun main(args: Array<String>) = runBlocking<Unit> { val job = Job() // create a job object to manage our lifecycle // now launch ten coroutines for a demo, each working for a different time val coroutines = List(10) { i -> // they are all children of our job object launch(coroutineContext + job) { // we use the context of main runBlocking thread, but with our own job object delay((i + 1) * 200L) // variable delay

How to call Kotlin suspending coroutine function from Java 7

你说的曾经没有我的故事 提交于 2020-03-17 10:50:13
问题 I'm trying to call Kotlin function from Java 7. I'm using coroutines and this called function is suspending, for example: suspend fun suspendingFunction(): Boolean { return async { longRunningFunction() }.await() } suspend fun longRunningFunction() : Boolean { delay(400) return true } I was using coroutines in version 0.25.3 and I could emulate simple Java callback style by passing Continuation<U> instance as an argument to suspending function, e.g. CoroutinesKt.suspendingFunction(new

Suspend function 'callGetApi' should be called only from a coroutine or another suspend function

和自甴很熟 提交于 2020-01-31 04:25:05
问题 I am calling suspended function from onCreate(...) override fun onCreate(savedInstanceState: Bundle?) { ... ... callGetApi() } and the suspended function is:- suspend fun callGetApi() {....} But the error shows up Suspend function 'callGetApi' should be called only from a coroutine or another suspend function 回答1: Suspend function should be called only from coroutine. That means you need to use a coroutine builder, e.g. launch . For example: class Activity : AppCompatActivity(),

Cancel file upload (retrofit) started from coroutine kotlin android

你说的曾经没有我的故事 提交于 2020-01-24 21:18:07
问题 I'm trying to get rid of RxJava2 in my project and replace it with kotlin coroutines. 90% of my RxJava code is no longer exists but I still can`t replace one network request. User can send photo to backend in my App (multipart form data request using retrofit). User also can cancel photo upload if it was not loaded yet. With RxJava I was able to keep Disposable object of upload request and if it is not disposed yet I could dispose it if user clicked cancel upload button. As result of this

Kotlin Coroutines - How to block to await/join all jobs?

风流意气都作罢 提交于 2020-01-24 06:01:05
问题 I am new to Kotlin/Coroutines, so hopefully I am just missing something/don't fully understand how to structure my code for the problem I am trying to solve. Essentially, I am taking a list of strings, and for each item in the list I want to send it to another method to do work (make a network call and return data based on the response). ( Edit :) I want all calls to launch concurrently, and block until all calls are done/the response is acted on, and then return a new list with the info of

Is this implementation of takeWhileInclusive safe?

白昼怎懂夜的黑 提交于 2020-01-22 03:38:06
问题 I found the following implementation of an inclusive takeWhile (found here) fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> { var shouldContinue = true return takeWhile { val result = shouldContinue shouldContinue = pred(it) result } } The problem is I'm not 100% convinced this is safe if used on a parallel sequence . My concern is that we'd be relying on the shouldContinue variable to know when to stop, but we're not synchronizing it's access. Any insights? 回答1:

Kotlin coroutine future await with timeout (no cancellation)

懵懂的女人 提交于 2020-01-15 11:27:10
问题 Given we have a CompletableFuture f , in kotlin suspendable scope we can call f.await() and we will suspend until its done. I'm having trouble implementing a similar function with signature f.await(t) which must suspend for maximum t milliseconds or return sooner if future did complete within that duration (whichever happens first). Here is what i tried. /** * Suspend current method until future is done or specified duration expires, * whichever happens first without cancelling the future. *

How to implement natural (aka. smart) batching with Kotlin channels?

巧了我就是萌 提交于 2020-01-13 10:25:35
问题 Natural aka. smart batching is a technique in stream processing that optimizes throughput without affecting latency. On the example of a concurrent queue, the consumer has the ability to atomically drain all the items observed at some instant and then process them as a batch. Ideally, the queue should be bounded, giving an upper limit to the batch size and providing backpressure to the sender at the same time. It's called "natural" batching because there's no imposed batch size: when the

kotlin, coroutines, NoSuchMethodError when calling overriden suspended function from other module

泪湿孤枕 提交于 2020-01-04 02:38:10
问题 Whenever I try to execute overrided suspend method which is defined in other module it fails with NoSuchMethodError (see more details below). Here what I am doing exactly: I have two modules app and lib . lib here I have an interface ( OtherModuleInterface ) with suspend method ( test ). app here I have defined implementation ( OtherModuleImpl ) of that interface. If I call it like this: val myOtherModuleObject : OtherModuleImpl = OtherModuleImpl() runBlocking { myOtherModuleObject.test() }