【发布时间】:2017-11-13 11:57:56
【问题描述】:
我有很小的机会(如果信任fabric crashreport,当天有0.5% 的用户)在RecyclerView 的两种方法中捕获IndexOutOfBoundsException: Inconsistency detected(我在fabric 中看到了崩溃日志):
- validateViewHolderForOffsetPosition()
- 尝试GetViewHolderForPositionByDeadline()
我在其他情况下搜索了相同的问题,我意识到从不同线程检索和更新适配器中的数据时可能会出现此问题。我傻乎乎地解决了这个问题,但理论上这可以正常工作:
private fun setUpdates() {
//...
//fix code start
if (Looper.getMainLooper().thread == Thread.currentThread()) {
log("thread is main")
adapter.notifyDataSetChanged()
} else {
log("thread NOT is main")
notifyOnUi()
}
//fix code end
//...
}
private fun notifyOnUi() {
Handler(Looper.getMainLooper()).post {
log("notify data set changes on UI")
adapter.notifyDataSetChanged()
}
}
而且我不知道抛出此异常时如何重现情况。我尝试了多个线程的不同选项,在adapter.notifyDataSetChanges() 之前/之后/期间修改列表,但这个问题不会重现(没有修复代码)。
还有一个问题——我们能做些什么来为RecyclerView 中的两个方法抛出IndexOutOfBoundsException: Inconsistency detected. ... 进行测试——是否修复我的代码这个问题?:
- validateViewHolderForOffsetPosition()
- 尝试GetViewHolderForPositionByDeadline()
【问题讨论】:
标签: android android-recyclerview android-adapter