val drawable = mutableStateOf<Drawable?>(null)
coroutineScope.launch {
drawable.value = app.requireIcon(activity)
}
But the code inside
launch
does not even start. What am I doing wrong?
I need to load a Drawable inside each lazy item and it can take a few seconds so I cannot do it on the main thread.
a
Alexander Karkossa
04/20/2021, 7:52 PM
You should not "launch" directly inside the compose world, because it could compose multiple times. Have you tried Coil für imageloading?
https://github.com/google/accompanist
y
YASAN
04/20/2021, 7:54 PM
I cannot use Coil - I have a Drawable object and Coil does not let me load a Drawable unless its a Resources Drawable.
YASAN
04/20/2021, 7:57 PM
My problem actually showing the image, I need to load the drawables from package manager which takes time. I am trying to do that on a background thread now. After I manage that I have to deal with loading it into the view properly.
s
steelahhh
04/20/2021, 8:36 PM
Using LaunchedEffect instead of scope directly will probably help, since it will only execute on composition, or whenever the passed in key changes
👀 1
steelahhh
04/20/2021, 8:37 PM
Also you need to
remember
the drawable because without it, it will reset after recomposition, so do this:
Copy code
var drawable by remember { mutableStateOf<Drawable?>(null) }
👀 1
y
YASAN
04/20/2021, 9:15 PM
Thanks. Although I actually found a better way to load the drawables. Not really a compose way, I just load all the images in the list in the viewmodel.
No matter what I tried I could not keep the loaded drawable in the actual object (could not reuse it in different screens. Since I need the drawables almost everywhere and reloading them from package manager is heavy, this makes more sense.
Although I am worried about memory usage now.
YASAN
04/20/2021, 9:15 PM
I did read a little bit about LaunchedEffect, did not properly understand really - will take a second look.