hey, i have written a little android example app (...
# android
c
hey, i have written a little android example app (my first one ever ­čśä) with jetpack-compose. since i need to use a blocking client to fetch my data to fulfill my use-case i am wondering how to do that. what i tried is to just call my client in a suspend function / coroutine and make the data available to my composables via a viewModel like this:
Copy code
class 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?
j
the general recommendation is to make suspend functions like that "main safe" by using something like
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
for your
fetch()
method
c
exactly what i just found here: https://developer.android.com/codelabs/kotlin-coroutines#7 ­čÖé but now my app is just dying with following in the logs
2021-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 much
j
can you see full stack trace in logcat ?
c
i copied this from logcat. it only shows one line regsarding the exception. no stacktrace ­čś× I tested the fetcher code in a backend project and it works fine there.
j
I cloned your repo (and switched to
android-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?
c
Whooot :D nice. This is exactly what it should do. Awesome. Whenever I click the load-users button the app is dying
Maybe something wrong with my local setup / emulator?
j
It's hard to tell without more detailed error info or repro
I'm running on emulator here fwiw
c
Sure. Ok, but that means generally the app code is working and I have to have a look at my local setup I would say. What emulator are u using (ddvice, sdk, android version)
j
Just tried on real device (Pixel 4a 5g) as well and worked on that too
also...using Arctic Fox beta 2 version of Android Studio
My Emulator was API level 29 and phone was 30
c
Strange I am using pixel 4 api level 30 emulator. OK I will try to find out what's going on with my emulator :) thx a lot for your your help ­čÖĆ
­čĹŹ 1