Question: I am making a program using Mapbox. I want to make my app robust, and handle cases were we don t have internet connection. This

I am making a program using Mapbox. I want to make my app robust, and handle cases were we dont have internet connection. This is how I observe internet connectivity in my viewmodel:
@HiltViewModel
class NetworkConnectivityViewModel @Inject constructor(private val connectivityObserver: NetworkConnectivityObserver) :
ViewModel(){
private var initialized = false
private val _connectionUIState =
MutableStateFlow(NetworkConnectivityObserver.Status.NoStatus)
val connectionUIState: StateFlow =_connectionUIState
@MainThread
fun initialize(){
if (initialized) return
initialized = true
viewModelScope.launch {
connectivityObserver.observe().collect { status ->
_connectionUIState.value = status
}
}
}
}
In another class, Ive made sure the map is not initialized if we do not have internet connection. However, I also want to handle the possibility of losing internet access while we are initializing the map. What is the best way to do that? Is it a good idea to use suspend functions and flags? Please provide simple examples.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!