我正在从 Paging 2 迁移到 Paging 3。该应用程序使用 Room 将大型数据集存储在数据库中,我可以从 Room 加载数据并正常显示。我遇到的问题是,一旦应用程序对数据库进行了更改,它就会崩溃。
代码片段
IssueRepository
@Query("SELECT * FROM isssue WHERE project_id = ?")
fun findAllInProject(projectId:Int): PagingSource<Int, IssueListBean>
在函数中onCreateView
val dataSource = DB.store.issueRepository().findAllInProject(project.id)
val pageConfig = PagingConfig(50)
val pager = Pager(pageConfig, null) { dataSource }.flow
viewLifecycleOwner.lifecycleScope.launchWhenCreated {
pager.collectLatest { data ->
adapter.submitData(data)
}
}
class PagingAdapter : PagingDataAdapter<IssueListBean, PagingAdapter.ViewHolder>(EntityComparator()) {
inner class ViewHolder(private val adapterBinding: ItemIssueListBinding) : RecyclerView.ViewHolder(adapterBinding.root) {
fun bind(position: Int) {
val issueListBean = getItem(position)
adapterBinding.label.text = issueListBean.label
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ItemIssueListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(position)
}
}
因此,当用户点击某个项目时,他们可以对其进行编辑。一旦通过 Room 保存该项目,应用程序就会崩溃,但出现以下异常:
java.lang.IllegalStateException: An instance of PagingSource was re-used when Pager expected to create a new
instance. Ensure that the pagingSourceFactory passed to Pager always returns a
new instance of PagingSource.
我用错了第 3 页吗?我在网上找不到很多关于使用 Room 作为您进行更改的数据源的文章。