Ale*_*hov 5 android kotlin android-studio
大概有两个片段:
// Verbose version
val typedArray = context.obtainStyledAttributes(attrs, styleable)
block(typedArray)
typedArray.recycle()
Run Code Online (Sandbox Code Playgroud)
// One-line version
context.obtainStyledAttributes(attrs, styleable).also(block).recycle()
Run Code Online (Sandbox Code Playgroud)
我想知道为什么冗长的代码块在Android Studio中看起来不错,而单行版本突出显示了getStyleStyledAttributes并给出以下警告:
与#recycle()一起使用后,应回收此TypedArray。
有谁知道这仅仅是一个Android Studio的绒毛检查缺陷,还是单行版本实际上有问题?
这有点猜测,但我相信这是因为静态分析工具不能保证返回TypedArray
的also()
与返回的实例相同obtainedStyledAttributes()
。它看到获得了 TypedArray,但不能保证它被回收,因此它发出警告。
我不太愿意将此称为 linter 中的错误(或“缺陷”),但这确实意味着在这种情况下可以忽略警告。
归档时间: |
|
查看次数: |
82 次 |
最近记录: |