andfun-kotlin-dev-bytes icon indicating copy to clipboard operation
andfun-kotlin-dev-bytes copied to clipboard

Crash when in offline mode and launch the app

Open alirezaeiii opened this issue 5 years ago • 7 comments

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

alirezaeiii avatar May 09 '19 11:05 alirezaeiii

Same crash is happening to me

PragyaSingla avatar May 17 '19 08:05 PragyaSingla

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" />

robert-gruner avatar May 19 '19 18:05 robert-gruner

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")
       }
}

StanGomes avatar May 19 '19 18:05 StanGomes

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.

rachmanabdau avatar Jun 02 '19 19:06 rachmanabdau

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 avatar Nov 18 '19 20:11 Kakomis

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

alirezaeiii avatar Nov 24 '19 10:11 alirezaeiii

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.

jsouza678 avatar Apr 08 '20 02:04 jsouza678