kotlin-coroutines

viewModelScope not cancelled

拜拜、爱过 提交于 2021-01-27 05:56:20
问题 After watching Sean's explanation on Android (Google I/O'19) I've tried the same: init{ viewModelScope.launch { Timber.i("coroutine awake") while (true){ delay(2_000) Timber.i("another round trip") } } } Unfortunately onCleared it's called when the activity is killed but not when it's put in background ("when we move away from the Activity...", background is "moving away" imho ^^). And I get the following output: > ---- Activity in Foreground > 12:41:10.195 TEST: coroutine awake > 12:41:12

Convert callback hell to deferred object

ⅰ亾dé卋堺 提交于 2021-01-27 05:28:50
问题 Background : So, I have a pretty big project with a lot of API functions. I'm thinking of completely moving to coroutines, but as they are implemented as Callback and not Deferred , I can not use them efficiently. For instance: I would like to do apiCallOne() , apiCallTwo() and apiCallThree() async and call .await() to wait till the last request is completed before changing UI. Now the project is structured like this: At the very bottom (or top) is ApiService.java : interface ApiService {

Convert callback hell to deferred object

隐身守侯 提交于 2021-01-27 05:25:50
问题 Background : So, I have a pretty big project with a lot of API functions. I'm thinking of completely moving to coroutines, but as they are implemented as Callback and not Deferred , I can not use them efficiently. For instance: I would like to do apiCallOne() , apiCallTwo() and apiCallThree() async and call .await() to wait till the last request is completed before changing UI. Now the project is structured like this: At the very bottom (or top) is ApiService.java : interface ApiService {

Convert callback hell to deferred object

烈酒焚心 提交于 2021-01-27 05:25:26
问题 Background : So, I have a pretty big project with a lot of API functions. I'm thinking of completely moving to coroutines, but as they are implemented as Callback and not Deferred , I can not use them efficiently. For instance: I would like to do apiCallOne() , apiCallTwo() and apiCallThree() async and call .await() to wait till the last request is completed before changing UI. Now the project is structured like this: At the very bottom (or top) is ApiService.java : interface ApiService {

Kotlin Coroutines with timeout

拟墨画扇 提交于 2021-01-21 05:09:28
问题 I'm currently writing a test-function which should run a block or (when a certain timeout is reached) throws an exception. I was trying this with Coroutines in Kotlin but ended up with a mixture of Coroutines and CompletableFuture : fun <T> runBlockWithTimeout(maxTimeout: Long, block: () -> T ): T { val future = CompletableFuture<T>() // runs the coroutine launch { block() } return future.get(maxTimeout, TimeUnit.MILLISECONDS) } This works, but I'm not sure if this is the intended way to

Webflux Kotlin Coroutines Flow doesn't return any results

倖福魔咒の 提交于 2021-01-07 06:41:13
问题 My Spring repository implement a function to return a kotlinx.coroutines.flow.Flow of User but it seems this flow is always empty even if there are some record in my DB. I am using Spring Boot 2.2.0-SNAPSHOT with the Kotlin coroutines support. I created two methods in my repository, one to create an user and one to list all users. The one to create an user works and I can see this user in my DB. The second one to list existing users returns an empty list, always, even if my DB has some

How to handle race conditions in kotlin, when listening to a firebase database node

雨燕双飞 提交于 2021-01-07 06:31:09
问题 Today, when I was working on my android project, I saw this weird behavior of my app. When I click on an icon and move to a different activity, which would basically insert another child into the node to which first activity was listening to. But to my surprise, when return back, everything in the screen was doubled. When I refresh the view, it again would become normal. I repeated it many times, thinking it may be a glitch. But same result every time. When I look into database, new child was

How To Use Async/Await/Coroutines In OnCompleteListener Firebase

穿精又带淫゛_ 提交于 2021-01-05 12:01:59
问题 I am building a client application which uses Firebase for two things: User Authentication Using a realtime database I have managed to set up everything correctly on my client and on my backend server (using Firebase's Admin SDK) and am able to correctly authenticate users and allow them to read/write to the database. I am also using Retrofit2 to send requests from the client to the backend. As part of allowing users access to the database, it is needed to send the user's token to the backend

Kotlin Coroutine Scope : Is return@runBlocking an issue if used in Controller Endpoint

自作多情 提交于 2021-01-05 08:59:25
问题 Purpose: I want to code an Endpoint which consume another Endpoint taking advantage of light Coroutines assuming I am coding a light assyncronous EndPoint client. My background: first time trying to use Kotlin Coroutine. I have studied last days and search around. I found numerous article explaining how use Coroutine in Android and I found few others explaining how use Coroutine in a main function. Unfortunatelly I didn't find articles explaining how code a Controller endpoint with coroutines

Kotlin MutableStateFlow.collect is dropping values

♀尐吖头ヾ 提交于 2021-01-05 07:08:51
问题 I have an android app in which I'm trying to use coroutine flows to replace the existing Otto EventBus using my own event bus library. I'm seeing dropped values when setting the MutableStateFlow's value and then collecting in my app's code. I've created a much simpler project that demonstrates the same issue. Any thoughts on why values are being dropped from the MutableStateFlow? Project's build.gradle buildscript { ext.kotlin_version = "1.4.10" repositories { google() jcenter() }