kotlin-coroutines

Kotlin coroutine flow example for Android button click event?

与世无争的帅哥 提交于 2021-02-07 02:56:01
问题 I used to use Channel to send out click event from Anko View class to Activity class, however more and more Channel functions are marked as deprecated. So I wanted to start using Flow apis. I migrated code below: private val btnProduceChannel = Channel<Unit>() val btnChannel : ReceiveChannel<Unit> = btnProduceChannel // Anko button { onClick { btnProduceChannel.send(Unit) } } to: lateinit var btnFlow: Flow<Unit> private set button { btnFlow = flow { onClick { emit(Unit) } } } I have to mark

How to test Coroutine with ViewModel in Kotlin?

不问归期 提交于 2021-02-05 09:16:10
问题 I could not test my method it feels it doesn't reach inside uiScope.launch block and I have posted my viewModel method which I am trying to test and fetchActivationCodeWithDuration is suspend function. and in the bottom is my test class and I get this message java.lang.AssertionError: Expected :ActivationCode(code=111111, expires=2019-05-23T10:03:50.614Z, duration=815960) Actual :null protected val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) fun loadActivationCode() { uiScope

How to test Coroutine with ViewModel in Kotlin?

浪尽此生 提交于 2021-02-05 09:16:02
问题 I could not test my method it feels it doesn't reach inside uiScope.launch block and I have posted my viewModel method which I am trying to test and fetchActivationCodeWithDuration is suspend function. and in the bottom is my test class and I get this message java.lang.AssertionError: Expected :ActivationCode(code=111111, expires=2019-05-23T10:03:50.614Z, duration=815960) Actual :null protected val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob) fun loadActivationCode() { uiScope

Android kotlin task to be executed using coroutines

岁酱吖の 提交于 2021-01-28 09:00:56
问题 As an example, I'm using FusedLocationProviderClient to access the current location, which returns a task which callback will eventually return the location. The method looks something like follows: fun getLocation(callback: MyCallback){ val flpc = LocationServices.getFusedLocationProviderClient(it) flpc.lastLocation.addOnSuccessListener { callback.onLocation(it) } } Is it possible to transform this so that I can use corroutines to suspend this function and wait for the task returned by flpc

Testing Coroutines runBlockingTest time

泄露秘密 提交于 2021-01-28 06:02:43
问题 Using this manual to test Coroutines using runBlockingTest . I encountered some issues. If the coroutine created by launch or async calls delay then the runBlockingTest will not auto-progress time right away. This allows tests to observe the interaction of multiple coroutines with different delays. @Test fun testFooWithLaunchAndDelay() = runBlockingTest { foo() // the coroutine launched by foo has not completed here, it is suspended waiting for delay(1_000) advanceTimeBy(1_000) // progress

ViewModel Unit testing multiple view states with LiveData, Coroutines and MockK

Deadly 提交于 2021-01-28 03:10:53
问题 I have a function in ViewModel with 2 states, first state is always LOADING, second state depends on result of api or db interactions. This is the function fun getPostWithSuspend() { myCoroutineScope.launch { // Set current state to LOADING _postStateWithSuspend.value = ViewState(LOADING) val result = postsUseCase.getPosts() // Check and assign result to UI val resultViewState = if (result.status == SUCCESS) { ViewState(SUCCESS, data = result.data?.get(0)?.title) } else { ViewState(ERROR,

Having coroutine wait for previous calls

帅比萌擦擦* 提交于 2021-01-27 20:16:03
问题 I still haven't fully grasped Kotlin coroutines yet. Basically I want a coroutine to wait for any previous calls to finish before executing. The following code seems to work. But is it doing what I think it's doing? private var saveJob: Job? = null fun save() { saveJob = someScope.launch { saveJob?.join() withContext(Dispatchers.IO) { // suspending database operation } } } As far as I can tell, the code is doing what I want. But is it? 回答1: Keep in mind that the launch ed code is concurrent

When do we use launch(SupervisorJob())?

荒凉一梦 提交于 2021-01-27 20:10:20
问题 I have seen tutorials passing SupervisorJob to CoroutineScope to avoid all coroutine jobs being cancelled if one of its child fails. In run3 , I thought passing SupervisorJob to launch can get the same result, but apparently it does not. It seems to allow the Coroutine to be reused if there is an exception (if you remove the SupervisorJob from launch , second run2 call won't run the coroutine job), but it dose not behave like supervisorScope , whose other children job can continue (in the

Listen to Kotlin coroutine flow from iOS

倖福魔咒の 提交于 2021-01-27 06:43:20
问题 I have setup a Kotlin Multiplatform project and attached a SQLDelight database to it. Its all setup and running correctly as i have tested it on the android side using the following: commonMain: val backgroundColorFlow: Flow<Color> = dbQuery.getColorWithId(BGColor.id) .asFlow() .mapToOneNotNull() which triggers fine in the Android projects MainActivity.kt using: database.backgroundColorFlow.onEach { setBackgroundColor(it.hex) }.launchIn(lifecycleScope) but when trying to access the same call

Listen to Kotlin coroutine flow from iOS

半世苍凉 提交于 2021-01-27 06:42:09
问题 I have setup a Kotlin Multiplatform project and attached a SQLDelight database to it. Its all setup and running correctly as i have tested it on the android side using the following: commonMain: val backgroundColorFlow: Flow<Color> = dbQuery.getColorWithId(BGColor.id) .asFlow() .mapToOneNotNull() which triggers fine in the Android projects MainActivity.kt using: database.backgroundColorFlow.onEach { setBackgroundColor(it.hex) }.launchIn(lifecycleScope) but when trying to access the same call