[在回收站视图列表中使用协程asLiveData时如何避免IndexOutOfBoundsException

问题描述 投票:0回答:1

我正在使用.asLiveData(viewModelScope.coroutineContext + Dispatchers.IO)从数据库中检索Flow数据,以填充放入Recyclerview ListAdapter中的列表...这是我的代码:

private var getList = { start: Date ->
    getListUseCase(Id!!, start, end!!, query)
        .map {
            it as PagedList
        }.asLiveData(viewModelScope.coroutineContext + Dispatchers.IO)
}

val List: LiveData<PagedList<Type>> =
    Transformations.switchMap(start, getList)

并且列表像这样绑定到recyclerview:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleList"
        adapter="@{adapter}"
        pagedListAdapterData="@{viewModel.List}" />

有顺次单击以更改开始和结束,然后更改列表,问题是如此快速地单击项目时,java.lang.IndexOutOfBoundsException:索引:-1,大小:9被捕获,应用程序崩溃。] >

val onItemClicked: (Int) -> Unit = {
    navigate(Navigation.PlanningToDetails(List.value!![it]!!))
}

那么如何处理呢? getListUseCase在IO线程上执行时,有没有办法阻止ui线程?还是有另一种解决方案,以使用例执行期间recyclerview不可点击?您将如何解决?

我正在使用.asLiveData(viewModelScope.coroutineContext + Dispatchers.IO)从数据库中检索Flow数据,以填充放入Recyclerview ListAdapter中的列表...这是我的代码:private var ...

android kotlin android-recyclerview indexoutofboundsexception kotlin-coroutines
1个回答
0
投票

为了避免异常,您应该检查所需的索引是否在范围内。

© www.soinside.com 2019 - 2024. All rights reserved.