andfun-kotlin-dev-bytes
andfun-kotlin-dev-bytes copied to clipboard
Crash when in offline mode and launch the app
When we are in offline mode, and I launch the app ( create a new instance of application ), app is crashing and this is what I receive in Logcat :
HTTP FAILED: java.net.UnknownHostException: Unable to resolve host "host": No address associated with hostname
2019-05-09 15:32:51.531 32728-32728/com.example.android.devbyteviewer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.devbyteviewer, PID: 32728
Same crash is happening to me
I guess the application code is not really supposed to support "being offline". It just tries to fetch the playlist via network regardless - this cannot work.
I worked around the problem via this change in the ViewModel's init block.
init {
val cm = application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
val isConnected: Boolean = activeNetwork?.isConnected == true
if (isConnected) {
viewModelScope.launch {
videosRepository.refreshVideos()
}
}
}
Keep in mind that you need another permission to access the state:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
My solution was to wrap the coroutine in refreshVideos() function with a try/catch block.
suspend fun refreshVideos() {
withContext(Dispatchers.IO) {
try{
val playlist = Network.devbytes.getPlaylist().await()
database.videoDao.insertAll(*playlist.asDatabaseModel())
} catch (e: Exception){
Timber.e("Updated playlist not available")
}
}
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file.
You can see the code here:
DevByteViewModel.txt DevByteFragment.txt
Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer
I also mark the code changes with TODO so you can easily find those code.
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file.
You can see the code here:
DevByteViewModel.txt DevByteFragment.txt
Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer
I also mark the code changes with TODO so you can easily find those co
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file.
You can see the code here:
DevByteViewModel.txt DevByteFragment.txt
Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer
I also mark the code changes with TODO so you can easily find those code.
I'm not sure, but Is it OK to pass a context to a ViewModel?
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file. You can see the code here: DevByteViewModel.txt DevByteFragment.txt Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer I also mark the code changes with TODO so you can easily find those co
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file. You can see the code here: DevByteViewModel.txt DevByteFragment.txt Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer I also mark the code changes with TODO so you can easily find those code.
I'm not sure, but Is it OK to pass a context to a ViewModel?
@Kakomis You need to use AndroidViewModel instead of ViewModel to pass a context. As a sample you can look at : MainViewModule.kt
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file. You can see the code here: DevByteViewModel.txt DevByteFragment.txt Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer I also mark the code changes with TODO so you can easily find those co
You can fix this crash by adding some line of code. Before you refresh the video database, you should check first is the internet connection available or not. If connection is not available don't update database, just populate video with data which is already in your database. You can add the code in DevByteViewModel.kt and DevByteFragment file. You can see the code here: DevByteViewModel.txt DevByteFragment.txt Or if you want to see full project you can see my github repo here: https://github.com/rachmanabdau/devbyte-viewer I also mark the code changes with TODO so you can easily find those code.
I'm not sure, but Is it OK to pass a context to a ViewModel?
Good question. The idea is that ViewModel doesn´t have to contain ANY component related to the view, so if you want to access the Context, you need to do like @robert-gruner did on his response.
And to find Application attribute on your ViewModel, you need to create an Factory Method.