最近我在我的应用程序中发现了奇怪的崩溃。我发现它们是由下面的 ListAdapter -> DiffUtil 引起的。契约(Contract)规定,仅当 areItemsTheSame 为相应项目返回 true 时,才会调用 areContentsTheSame 回调。
问题是为从未调用过 areItemsTheSame 的项目调用 areContentsTheSame。
我正在 String 项目上测试它,所以它不应该与我自己的回收器实现相关。我真的很困惑,如果这是我的错(现在几乎没有逻辑)或 DiffUtil 工具中的错误
我已经创建了简单的 Instrumented Test,但在上述情况下失败了 - 更有经验的人可以看一下吗:
package com.example.diffutilbug
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import junit.framework.Assert.assertTrue
import kotlinx.coroutines.*
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.BlockJUnit4ClassRunner
@RunWith(BlockJUnit4ClassRunner::class)
internal class ExampleUnitTest {
@Test
fun testDiffUtil4() {
val handler = CoroutineExceptionHandler { _, exception ->
throw exception
}
// adapter compare items :
// areItemsTheSame -> compare length of String
// areContentsTheSame -> compare content with ==
val adapter = StringAdapterJunit(handler)
runBlocking {
adapter.submitList(
mutableListOf<String>(
"1",//1,
"22",//2,
"333",//3,
"4444",//4,
"55555",//5,
"666666",//6,
"7777777",//7,
"88888888",//8,
"999999999",//9,
"55555",//5,
"1010101010",//10,
"1010109999",//10,
"55555",//5,
"1313131313",//10,
"1414141414",//10,
"55555",//5,
"1313131313",//10,
"1414141414",//10,
"55555"//5
)
)
delay(40)
adapter.submitList(
mutableListOf<String>(
"55555",//5,
"1010101010",//10,
"1010109999",//10,
"55555",//11,
"1313131313",//10,
"1414141414",//10,
"11111111111"//11
)
)
delay(500)
}
}
}
// Stub Adapter for Strings that uses DiffUtil underneath.
// logs all callbacks to logcat
class StringAdapterJunit(val handler: CoroutineExceptionHandler) : ListAdapter<String, RecyclerView.ViewHolder>(object : DiffUtil.ItemCallback<String>() {
override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
Log.e("DiffUtilTest", "areItemsTheSame comparing $oldItem with $newItem = ${oldItem.length == newItem.length}")
return oldItem.length == newItem.length
}
override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
//should be called only if areContentsTheSame == true
Log.e(
"DiffUtilTest",
"areContentsTheSame error = ${oldItem.length != newItem.length} comparing $oldItem with $newItem"
)
runBlocking {
GlobalScope.launch(handler + Dispatchers.Main) {
assertTrue("areContentsTheSame can be called only if areItemsTheSame return true" , areItemsTheSame(oldItem, newItem))
}.join()
}
return oldItem == newItem
}
override fun getChangePayload(oldItem: String, newItem: String): Any? {
//should be called only if areItemsTheSame = true and areContentsTheSame = false
Log.e(
"DiffUtilTest",
"getChangePayload error = ${oldItem.length == newItem.length && oldItem == newItem} $oldItem with $newItem"
)
return null
}
}) {
// stub implementation on adapter - never used
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = object : RecyclerView.ViewHolder(View(null)) {}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {}
override fun getItemViewType(position: Int): Int = getItem(position).length
}
及其所需的 gradle 依赖项:
dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
//coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
}
请注意需要添加
android.useAndroidX=true
android.enableJetifier=true
在你的gradle.properties
添加了协程和异常处理程序,因为 DiffUtil 在后台线程上计算差异,而 JUnit 仅在主线程上处理断言
============================================= ======
在下一个 alpha 中修复: 将在 alpha 3 中发布 - PR 以照顾 https://android-review.googlesource.com/c/platform/frameworks/support/+/1253271 谢谢,迫不及待地想删除所有解决方法!
最佳答案
我收到了 google 的回复,他们确认当列表包含重复项(空值、相同对象等)时 DiffUtil 中存在错误
我目前的解决方法是在执行前自己检查“契约(Contract)”,因此:
override fun areItemsTheSame(oldItem: String, newItem: String): Boolean {
return compare items
}
override fun areContentsTheSame(oldItem: String, newItem: String): Boolean {
//should be called only if areItemsTheSame == true
return areItemsTheSame(oldItem, newItem) && compare items contents
}
override fun getChangePayload(oldItem: String, newItem: String): Any? {
//should be called only if areItemsTheSame = true and areContentsTheSame = false
if(areItemsTheSame(oldItem, newItem) && !areContentsTheSame(oldItem, newItem)) {
return compute changePayload
} else {
return null
}
}
问题解决后会更新答案
关于android - DiffUtil 违反了 areContentTheSame 的契约(Contract) [下一版本将修复],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54364098/
以太坊价格分析表明横盘整理,偏向中性。价格从前一交易日的高点1,791美元回落后正在盘整。但是,有趣的是,多头在1,680美元附近持有重要支撑。多头在1,700美元的心理水平附近聚集动能,并准备在接下来的几个交易日推向1,800美元。以太坊价格显示出盘整迹象,因为它形成了多个连续的顶部形态。这种回撤可能是第二大加密货币下一轮上涨的基石。以太坊连续第二个交易日走低。过去10天,价格在1,590-1,760美元的短期区间内盘整。每日烛台高于1,800美元将维持ETH的进一步上涨。ETH价格走低日线图上,以太坊价格在上升趋势线附近获得一轮支撑。来自879.80美元低点的看涨趋势线为ETH买家提供了支
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
执行rvmlist后,我得到以下输出:rvmrubiesgems[missingbin/ruby]=*ruby-2.0.0-p645[x86_64]ruby-2.1.6[x86_64]ruby-2.2.1[x86_64]gems[missingbin/ruby]是什么意思?gems是某种系统gemset吗?它不是我创建的,我不知道我是否可以或应该删除它。 最佳答案 在我跑完之后:rvmfix-permissions然后我能够卸载具有[缺少bin/ruby]的版本。 关于ruby-如何修复
假设我有一个没有特定顺序的随机数数组。假设这些是参加马拉松比赛的人的ID#,他们按照完成的顺序添加到数组中,例如:race1=[8,102,67,58,91,16,27]race2=[51,31,7,15,99,58,22]这是一个简化且有些做作的示例,但我认为它传达了基本思想。现在有几个问题:首先,我如何获得特定条目之前和之后的ID?假设我正在查看运行者58,我想知道谁在他之前和之后完成了比赛。race1,runner58:previousfinisher=67,nextfinisher=91race2,runner58:previousfinisher=99,nextfinishe
我有一个依赖于另一个gem的gem(在RubyGems上可用)。那个依赖的gem有一个我最近修复的错误。不幸的是,那个依赖的gem几乎已经死了;它已经很多年没有更新了,而且所有者不再在GitHub上活跃,根本,更不用说提交这个gem了,所以我不希望我的补丁会被接受——肯定不会很快。鉴于此,处理此依赖gem的补丁版本的最佳方法是什么?我是否将它fork并上传一个新的gem(使用新名称)到RubyGems,并依赖它?我是否以某种方式将我的固定版本与我自己的gem打包在一起? 最佳答案 首先,检查有问题的gem的许可证(以及您的代码的许可
Facebook刚刚重新推出了具有自动语法修复功能的评论。Whatdoesthegrammarfilterdo?Addspunctuation(e.g.periodsattheendofsentences)TrimsextrawhitespaceAutocaseswords(e.g.capitalizethefirstwordofasentence)Expandsslangwords(e.g.plzbecomesplease)Addsaspaceafterpunctuation(e.g.Hi,CatwouldbecomeHi,Cat)Fixcommongrammarmistakes(e
我在尝试安装ruby和rails时遇到了很多问题。在清除以前安装的版本之后,我已经尝试过没有和现在。尝试运行“geminstallrdiscount--platform=ruby”时出现以下错误:C:\Windows\system32>geminstallrdiscount--platform=rubyTemporarilyenhancingPATHtoincludeDevKit...Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingrdiscount:ERROR:Failedtobuildgem
我似乎找不到一种优雅的方式来做到这一点......给定一个日期,我如何找到下一个星期二,即日历月的第2个或第4个星期二?例如:给定2012-10-19然后返回2012-10-23或给定2012-10-31然后返回2012-11-13OctoberNovemberSuMoTuWeThFrSaSuMoTuWeThFrSa12345612378910111213456789101415161718192011121314151617212223242526271819202122232428293031252627282930 最佳答案
我有以下文件结构:执行.rb图书馆我的类(class).rb在execute.rb我有下面的代码:#!/usr/bin/rubyrequire'lib/my_class'my_object=MyClass.newmy_object.some_method这是my_class.rb的代码:classMyClassdefsome_methodputs'OK'endend所以,我尝试运行execute.rb:rubyexecute.rb但是我收到这个错误:/home/vagrant/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/ru
我在我的Sinatra应用程序中使用Hash#to_xml。在我转向actviesupport3.0.0之前,它一直有效activesupport在3.0.0的使用有区别吗?例如这很好用gem'activesupport','2.3.5'require'active_support'{}.to_xml和gem'activesupport','3.0.0'require'active_support'{}.to_xml生成:NoMethodError:{}的未定义方法“to_xml”:哈希 最佳答案 当您需要时,ActiveSuppo