``` var toReturn: PixelArt? = null CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).laun...
t
Copy code
var toReturn: PixelArt? = null

    CoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch {
        AppData.pixelArtDB.pixelArtCreationsDao().getAllPixelArtCreations().observe(this@extendedGetCurrentPixelArtObj.findViewTreeLifecycleOwner()!!) {
            toReturn = it[currentIndex]
        }
    }

    return toReturn!!
🧵 2
u
you can’t observe in worker thread
t
K then what should I do instead, this is very confusing 2 me sir
Please give advice on how to structure this call/similar calls asynchronously
u
there are many ways to handle it. 1. create a variable in viewmodel (LiveData) and initialize it when you call room function that returns livedata and observe this variable in fragment or activity so when the room return data your live data that you are observing will produce data in fragment, 2. get Flow from room and use it asLiveData in viewModel and observe it in fragment one thing to note when you deal with flow or livedata do not expect your function to return any value directly
try this
t
Thank u g, appreciated