A year or so ago, I was having issues with LazyCol...
# compose-android
t
A year or so ago, I was having issues with LazyColumns where updates to its items were happening lazily from other coroutines, it was suggested I use this "solution"
Copy code
// "Ideally, we would run measure and layout in separate snapshots, so it would be consistent throughout the pass, but that breaks some existing code, unfortunately.
// What you can do is make sure that the state that is used in items  is only updated on the main thread." and then he offered the following modifier hack as well
// This should be used over LazyColumn IF (and probably only when) the items enumerated may be updated off of the main tread
@Composable
fun ThreadSafeLazyColumn(
    modifier: Modifier = Modifier,
    state: LazyListState = rememberLazyListState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    content: LazyListScope.() -> Unit,
) {
    LazyColumn(modifier = modifier.layout { m, c ->
       val placeable = Snapshot.withMutableSnapshot { m.measure(c) }
       layout(placeable.width, placeable.height) { placeable.place(IntOffset.Zero) }
    }, state = state, contentPadding = contentPadding, content = content)
}
Updating my app to all of the new versions, I'm wondering if this is still necessary? Or if newer versions have addressed these layout issues.
z
t
looks like it's still unresolved... i'll keep it for now then
z
You just said you were having “issues”. I have no idea what kind of issues so I have no idea if that bug even has anything to do with the code you posted.
s
We tried doing this in lazy layout itself, but it broke layout logic in some existing apps, so we decided to /not/ do that internally. The eventual solution is to run measure and layout / draw in a separate snapshot, but it is changing when measure / layout is called for existing apps / components, so I am not sure we will ever fix this in this way. It is mostly a problem caused by
derivedStateOf
, but I don't see how we can fix it without removing
derivedStateOf
completely.
z
Andrei you know if that buganizer related to this issue?
s
I don't think so, I think the OP crash was something about IntervalList size or something
👍🏻 1
t
@shikasd, you were the one that gave me that fix (I immortalized your name in the unshared part of the comment). I'm very grateful. Sounds like it's still an issue, despite zach's share not being relevant
(though zach is great sharer (still a big fan of that 7 part series on state in compose))
s
it is somewhat sad that it's been a year since that discussion, time flies
z
I remember filing a bug about this actually I think, but I missed the workaround so TIL