How to check internet connection during started coroutines job

My App downloads some info from server. At the begining I check the Internet connection then start job like:

...
    checkConnection{
         val data = withContext(Dispatchers.IO) { Repo.getInfo() }
    }
...

fun checkConnection( block: ()-> Unit){
    ...//check
    if (hasConnection) block() else showSnackbarInternetError()
}
  1. Check connection and start Repo.getInfo()
  2. During job Internet has been lost
  3. Job is suspended until Internet will appear

How should I check Internet connection with started job?
Perhaps, should I create some InternetListener?

1 Like