32 questions
0
votes
1
answer
209
views
Why ViewModel's StateFlow is not updating when inside a unit test and is mapped from a repository to UI state using stateIn?
I am testing a ViewModel using a fake repository which uses a StateFlow to store the fake data. This StateFlow is exposed as a normal Flow from the repository. In the ViewModel, I am mapping the ...
1
vote
1
answer
275
views
Flow doesn't emit in test when using Dispatchers.Main.immediate (or viewModelScope) to change the value of a StateFlow
I investigated for hours and searched the web (and even bothered ChatGPT) and I'm puzzled this hasn't been solved yet to my (obviously very limited) knowledge.
So this is the setup: I have a view ...
2
votes
0
answers
330
views
Why does kotlin flow 'onStart' method not call immediately after 'collect' is called?
example code:
suspend fun main() {
val start = System.currentTimeMillis()
flowOf("flow value") // 1
.onStart {
println("onStart delta: ${System.currentTimeMillis() - ...
2
votes
1
answer
544
views
Kotlin async not running tasks parallelly
I was trying Kotlin coroutines using a test file. What I observed is that the async{..} block , completes and then the next async block starts.
@Test
fun `xyz`() {
runBlocking {
val x = ...
0
votes
1
answer
171
views
Run coroutines concurrently without waiting for completion
I'm struggling to understand why this is so difficult to figure out. This is in Android if it matters. Please see the code below
private val _myFlow = MutableStateFlow<Result<Flow<...
0
votes
1
answer
828
views
Propagate exception from background thread to main thread with Kotlin Coroutines
With Kotlin Coroutines I need to run two I/O work inside a try block at same time parallely without blocking each other.
And if any of the work fails, then it should be caught inside a single catch ...
0
votes
1
answer
248
views
kotlin coroutines: how to avoid multiple calls of map() if we have 2 or more collectors
There is code sample:
fun main() {
runBlocking {
val sourceFlow = MutableSharedFlow<Int>()
val mappedFlow = sourceFlow
.map {
println("...
2
votes
1
answer
1k
views
Kotlin coroutines: flatMapLatest emits nothing
There is the sourceFlow and I use flatMapLatest to produce new flow by value from sourceFlow.
And there is no value in collect(). This code prints nothing.
fun main() {
fun createNewFlow(): ...
0
votes
1
answer
502
views
Using flows to return a list as a terminal operator
I have the following that returns a Flow<List<Events>>. But I want return the List<Events>> from the flow instead of Flow<List>
val events = eventDao.getEventsFromTimeStamp(...
1
vote
1
answer
217
views
Android Paging3 Room performance
I'm playing with Paging3 with Room. I wrote a little test app that populates a Room DB with a single table with 5 string fields (plus its id). I populate the table with 100,000 items.
The code for ...
2
votes
1
answer
3k
views
How to combine Kotlin flows only when the first is emitting a (not null) value, unsubscribing the second one if it is not?
What I'm trying to achieve is that flowB is just (re)subscribed when flowA emits a value different of null. But there is no standard way to do that (as I can see).
In my scenario, flowB is expensive ...
1
vote
0
answers
590
views
Test MutableSharedFlow with runTest
I’m having some trouble to test some state changes in my viewModel using MutableSharedFlow. For example, I have this class
class SampleViewModel : ViewModel() {
private val interactions = Channel&...
0
votes
0
answers
218
views
Cannot send in Coroutines Channel
I am practicing Coroutines Channel and I'm wondering why does the repository.getAllRates() did not trigger even my code reached the sellCurrencyChannel.send(CurrencyRate(“”, 0.0))? I'm also suspecting ...
1
vote
1
answer
2k
views
Unit test of method collecting infinite Flow in background runs forever
I am struggling test and/or implement a method that listens to an infinite flow in the background. Specifically, the use case I have in mind is a repository for some data that has a local and remote ...
2
votes
2
answers
2k
views
SQLDelight convert query return type when using flows
I want to use SQLDelight as a caching layer in my App with the coroutines extension to return a flow from my SQL queries and get notified when the entry in the local Database changes.
But because ...
1
vote
1
answer
2k
views
Accessing values outside a coroutine scope in Kotlin
I got this code right here, that works fine. I can print out the values i get from every job/coroutines that launches inside the scope. But the problem is that i struggle to use the values outside of ...
7
votes
1
answer
2k
views
Moving Window With Kotlin Flow
I am trying to create a moving window of data using Kotlin Flows.
It can be achieved in RxKotlin using a buffer, but buffer is not the same using Flows.
RxKotlin has a buffer operator, periodically ...
4
votes
1
answer
1k
views
Why should AsyncPagingDataDiffer submitData() freeze and timeout the test?
I'm trying to follow this documentation here concerning how to unit test a PagingData stream on which you're applying transforms. The code I am using is similar:
@ExperimentalCoroutinesApi
@Test
fun ...
3
votes
3
answers
6k
views
How to create a countdown with flow coroutines
I'm trying to create a flow with coroutines but it's not giving to me the expected result.
What I'd like to have is giving an expiration time (doesn't matter if it's in millis, seconds, etc..) when ...
1
vote
1
answer
6k
views
Problem with the new version of kotlinx-coroutines-play-services into my code
I already added coroutine play services into my code, everything was working find, but after updating coroutines to the version 1.3.2, my application crash and give me the error bellow, but into the ...
1
vote
1
answer
6k
views
How to cancel collect coroutine StateFlow?
I have collect flow from shared viewmodel in fragment :
private val viewModel: MyViewModel by sharedViewModel()
private fun observeViewModelStateFlowData() {
job = lifecycleScope.launch {
...
3
votes
1
answer
4k
views
Is it possible to consume text/event-stream with JavaScript in a WebBrowser?
We have a Rest Service that returns a text/event-stream from a POST endpoint, which contains a series of JSON Objects.
(It is a Spring Boot / Kotlin RestController that returns a kotlinx.coroutines....
4
votes
2
answers
6k
views
How to replace LiveData with Flow
I've one LiveData named sortOrder and then I've another variable named myData that observes any change to sortOrder and populates data accordingly.
class TestViewModel @ViewModelInject constructor() : ...
4
votes
2
answers
2k
views
SharedFlow : mapLatest not getting triggered
Let's make this simple. I've one MutableSharedFlow named sortOrder in my ViewModel.
private val sortOrder = MutableSharedFlow<String>(
replay = 0,
extraBufferCapacity = 1
)
I've a ...
4
votes
1
answer
2k
views
How to return fusedLocationProviderClient().lastLocation as flow from a function in kotlin coroutines
What i am doing
So i am developing a weather forecast application in which i am accessing the device location using fusedLocationProviderClient.lastLocation and most of us knows that the location ...
8
votes
2
answers
15k
views
Suspending function can only be called within coroutine body
I'm trying to deliver realtime updates to my view with Kotlin Flows and Firebase.
This is how I collect my realtime data from my ViewModel:
class MainViewModel(repo: IRepo): ViewModel() {
val ...
27
votes
3
answers
19k
views
How to cancel/unsubscribe from coroutines Flow
I notice a strange behavior when trying to prematurely cancel from a Flow. Take a look at the following example.
This is a simple flow that emits integer values
private fun createFlow() = flow {
...
17
votes
1
answer
16k
views
How to filter a list inside Kotlin Flow
I'm replacing my current implementation using RxJava to Coroutines and Flow. I'm having some trouble using some Flow operators.
I'm trying to filter the list of items inside a Flow before providing ...
43
votes
3
answers
19k
views
Kotlin Flow vs LiveData
In the last Google I/O, Jose Alcerreca and Yigit Boyar told us that we should no longer use LiveData to fetch data. Now we should use suspend functions for one-shot fetches and use Kotlin's Flow to ...
15
votes
3
answers
6k
views
Test methods of Room DAO with Kotlin Coroutines and Flow
I am trying to migrate from LiveData to Flow in my Room Dao. App is working fine, but I have problems with testing behavior. When I run the test it is starting and running indefinately.
I also tried ...
0
votes
0
answers
742
views
Flows - Cloning a flow without multiple iteration - am I doing it right?
I am just starting to familiarize myself with Kotlin flows.
For this, I am using them to parse the contents of a binary file which I will simulate using the following flow:
fun testFlow() = flow {
...
42
votes
3
answers
28k
views
How can I send items to a Kotlin.Flow (like a Behaviorsubject)
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 ...