I am trying to use `coroutines` in a `LazyVertical...
# compose
y
I am trying to use 
coroutines
 in a 
LazyVerticalGrid
 item. I have this:
Copy code
val coroutineScope = rememberCoroutineScope()
and this in a `Column`:
Copy code
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
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
I cannot use Coil - I have a Drawable object and Coil does not let me load a Drawable unless its a Resources Drawable.
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
Using LaunchedEffect instead of scope directly will probably help, since it will only execute on composition, or whenever the passed in key changes
👀 1
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
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.
I did read a little bit about LaunchedEffect, did not properly understand really - will take a second look.