Jan
04/27/2022, 5:52 PMLaunchedEffect(Unit) {
while(true) {
delay(7000)
try {
productMap.fill(ShoppingRepository.getProducts())
} catch(e: Exception) {
productMap.fill(cache)
e.printStackTrace()
}
}
}
Does this get stopped when the process is not in the foreground or something?Jan
04/27/2022, 6:13 PMOleksandr Balan
04/27/2022, 7:25 PMProcessLifecycleOwner
to observe when application goes to foreground / background.
For example you may setup the following in your Application
class:
override fun onCreate() {
super.onCreate()
val lifecycle = ProcessLifecycleOwner.get().lifecycle
lifecycle.addObserver(
object : DefaultLifecycleObserver {
private var job: Job? = null
override fun onStart(owner: LifecycleOwner) {
// Application goes to foreground, setup job
job = lifecycle.coroutineScope.launch {
while(isActive) {
delay(7000)
// Fetch & store
}
}
}
override fun onStop(owner: LifecycleOwner) {
// Application goes to background, cancel job
job?.cancel()
}
}
)
}
Orlando Novas Rodriguez
04/29/2022, 2:28 AM