'Collect Flows in Service

So, I'm trying to collect data from flows in my Foreground service (LifecycleService) in onCreate(), but after the first callback, it is not giving new data.

The code is :

    override fun onCreate() {
        super.onCreate()

        lifecycleScope.launchWhenStarted {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                observeCoinsPrices()
            }
        }
    }


Solution 1:[1]

Because Flow used in observeCoinsPrices() not replay the latest value (replay < 1). You should change Flow logic

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 Petrus Nguyễn Thái Học