简体   繁体   中英

LazyRow inside LazyColumn call recompose every time

Hi I have an object with a list of other objects. I need to show a list of the first one, and inside each object show a horizontal list with the other objects.

I have these object stored in local database by room. These objects are related with a cross ref:

data class ObjectWithOtherObjectsCrossRef(
    @ColumnInfo(name = "object_id")
    val objectId: Long,
    @ColumnInfo(name = "other_object_id")
    val otherObjectId: Long,
)

I have a lazyRow inside a lazyColumn. The lazyColumn is populated by a room flow. The first time I go to the screen, the view acts properly, but when I insert a new item, recompose is calling every time and the scroll became slow.

Any idea about why recompose is calling every time? Thanks!

Here is my code:

DataModel:

data class MyObject(
    val id: Long,
    val name: String,
    val list: List<OtherObject>
)

data class OtherObject(
    val id: Long,
    val name: String
)

UiState:

sealed class MyObjectState {
    object Loading: MyObjectState()
    data class Success(val data: List<MyObject>): MyObjectState()
}

ViewModel:

val uiState: StateFlow<MyObjectState> = getAllObjects().stateIn(
    scope = viewModelScope,
    started = SharingStarted.WhileSubscribed(5_000),
    initialValue = MyObjectState.Loading
)

fun addNewObject(object: Long, otherObject: Long) {
    viewModelScope.launch {
        addNewObjectUseCase(object, otherObject)
    }
}

Composable

@Composable
fun MainView() {
    val state: MyObjectState by viewModel.uiState.collectAsState()

    when (state) {
        MyObjectState.Loading -> {...}
        is MyObjectState.Success -> ListScreen(state.data)
}



@Composable
fun ListScreen(
    val list: List<MyObject>
) {
    LazyColumn {
        items(
            items = list,
            key = { it.id }
        ) { 
            Text(text = it.name)
            LazyRow {
                items(
                    items = it.list,
                    key = { it.id },
                ) { 
                    Text(text = it.name)
                }
            }
        }
    }
}

I tried downgrading the compose version to the stable one (1.1.1) and it works fine.

I was using version 1.2.0.beta02

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM