'Flow of location

I want to use a coroutine flow as a source of the current phone position and mark it on the map. Here what I have in my ViewModel:

private val gpsAwareLocationFlow = broadcastFlow(BroadcastTransformers.gpsReceiver)
    .flatMapConcat { locationNotifier.locationFlow() }

private val locationFlow = locationNotifier.locationFlow()

val currentPosition = //here is place for solution, I suppose 
    .asLiveData()

When I use locationFlow.asLiveData, activity receives data as long as the GPS option is on. When it's turned off stream is closed and activity doesn't react on GPS option disabled or enabled.

When I use gpsAwareLocationFlow.asLiveData(), activity reacts properly only after the first change of GPS option. Before the option is changed it doesn't do anything.

The first idea was to use combine on both flows. It turned out that as long as both work well on their own (at least as flow), after combining them, result flow doesn't receive any notifications.

    val resultFlow = locationFlow.combine(gpsAwareLocationFlow){ a, b-> }

Have anybody got any idea what am I doing wrong?



Solution 1:[1]

I suppose the problem is that combineLatest function doesn't emit anything until both flow sources don't emit at least one value. I good way to solve this is to initialize both flows with a value but I don't know if you can do this in your code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pierluigi