Can someone suggest what will be the best method t...
# android
a
Can someone suggest what will be the best method to periodically check Internet Connection after every 5 sec? ( Not work Manager as min time to use it is 15 min) and ( Also not looper as it will consume a thread permanently )
😶 7
g
I would do it with kotlin flows:
Copy code
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach


flow {
    while (true) {
        emit(isOnline())
        delay(5000L)
    }
}
    .onEach {
        println(it)
    }
    .launchIn(this.lifecycleScope)
a
Thanx problem resolved
g
A little edit, to make it cleaner, in my opinion, I would use isActive() instead of true to make sure the while loop is broken whenever the scope associated with the while loop is cancelled.