Christian Dräger
05/28/2021, 9:03 PMclass StargazerViewModel : ViewModel() {
private var _users: MutableLiveData<List<User>> = MutableLiveData(emptyList())
val users: LiveData<List<User>> = _users
fun updateUsers() {
viewModelScope.launch {
_users.postValue(fetch())
// _users.postValue(dummyData) // when using valid dummy data here the app works as aspected
}
}
}
private suspend fun fetch(): List<User> =
skrape(HttpFetcher) {
request {
url = "<https://github.com/skrapeit/skrape.it/stargazers>"
}
...
full code here: https://github.com/skrapeit/skrape.it/blob/android-example/examples/android/app/src/main/java/it/skrape/skrapeitexample/MainActivity.kt
whenever i call the fetch method i get a android.os.NetworkOnMainThreadException
which i assume is because my client call is blocking and thereby break the coroutine (maybe?)
what can i do to run the blocking client in a background thread to avoid the NetworkOnMainThreadException in a jetpack-compose project?John O'Reilly
05/28/2021, 9:43 PMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
for your fetch()
methodChristian Dräger
05/28/2021, 9:53 PM2021-05-28 23:51:20.556 4197-4197/it.skrape.skrapeitexample E/AndroidRuntime: FATAL EXCEPTION: main
since i am new to android, how can i find out what is the problem here? the error message is not saying muchJohn O'Reilly
05/28/2021, 9:54 PMChristian Dräger
05/28/2021, 10:00 PMJohn O'Reilly
05/28/2021, 10:09 PMandroid-example
branch) and app seemed to work....at least was able to launch and click load users and this was displayed. At what point to you see it crash?Christian Dräger
05/28/2021, 10:10 PMChristian Dräger
05/28/2021, 10:11 PMJohn O'Reilly
05/28/2021, 10:11 PMJohn O'Reilly
05/28/2021, 10:11 PMChristian Dräger
05/28/2021, 10:14 PMJohn O'Reilly
05/28/2021, 10:15 PMJohn O'Reilly
05/28/2021, 10:16 PMJohn O'Reilly
05/28/2021, 10:18 PMChristian Dräger
05/28/2021, 10:20 PM