question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty combine <Iterable<Flow<T>>> not emitting anything

See original GitHub issue

I just debugged an issue where I did not get any events from the flows combine function.

I had a code where I combined the latest values of a List<Observable<Recipe>> into an Observable<List<Recipe>> by using combineLatest.

I refactored that code to use flows’'s combine to convert a List<Flow<Recipe>> into a Flow<List<Recipe>>. However this never emitted an item if the data sources were empty.

I expect that the function:

@ExperimentalCoroutinesApi
public inline fun <reified T, R> combine(
    flows: Iterable<Flow<T>>,
    crossinline transform: suspend (Array<T>) -> R
): Flow<R> {
    val flowArray = flows.toList().toTypedArray()
    return flow {
        combineInternal(
            flowArray,
            arrayFactory = { arrayOfNulls(flowArray.size) },
            transform = { emit(transform(it)) })
    }
}

gives me an empty array in the transform method but it just hangs forever. (coroutines 1.3.2)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
psteigercommented, Jan 4, 2021

@mtrakal consider this more expressive code:

val currencies = currencyDao.getCurrencies()
    .onEmpty { emit(emptyList()) }
    .single()

I do agree that empty flows should not be confused with a flow with a default ‘empty value’. Maybe this should be Room-configurable, but I think Flow behavior here should not change.

Edit: I’m not sure why your Room flow is not emitting an empty list. Room docs suggest that should be the behavior:

Keep nullability in mind when choosing a return type, as it affects how the query method handles empty tables:

When the return type is Flow<T>, querying an empty table throws a null pointer exception.

When the return type is Flow<T?>, querying an empty table emits a null value.

When the return type is Flow<List<T>>, querying an empty table emits an empty list.

https://developer.android.com/reference/androidx/room/Query

2reactions
niquecocommented, Feb 1, 2020

It’s wrong to close this issue. I’ve just debugged my code and reached to this unexpected behavior (I was about to report this as a new bug). A “combine” that is passed an empty list of flows should emit once the current result status, which is an empty list. The current behavior will hit many users.

Read more comments on GitHub >

github_iconTop Results From Across the Web

itertools — Functions creating iterators for efficient looping ...
The combination tuples are emitted in lexicographic ordering according to the order of the input iterable. So, if the input iterable is sorted,...
Read more >
Personalizing Templates with Handlebars
Iterable message templates use Handlebars helpers and block helpers to display user and event profile data in a way that is most suitable...
Read more >
How to merge two maps into one with keys from the first map ...
How can I create a new map from two maps of maps so that the resulting map only includes matches where keys are...
Read more >
Python's map(): Processing Iterables Without a Loop
In this tutorial, you'll learn: How Python's map() works; How to transform different types of Python iterables using map(); How to combine map() ......
Read more >
Python TypeError: 'float' object not iterable Solution
The result of the “TypeError: 'float' object not iterable” error is usually trying to use a number to tell a for loop how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found