【问题标题】:Perfomance difference with reference in kotlin在 kotlin 中与参考的性能差异
【发布时间】:2020-10-12 10:57:47
【问题描述】:

我的问题是,当我在 kotlin 中使用引用与“正常”方法(没有)时是否存在任何性能差异。假设我们要向ListAdapter 提交一个列表,我们可以使用两种方法。

接近一

shoppingCartViewModel.productList.observe(viewLifecycleOwner) {
     it.let(shoppingCartAdapter::submitList)
}

方法二

shoppingCartViewModel.productList.observe(viewLifecycleOwner) {
     shoppingCartAdapter.submitList(it)
}

首选的方法应该是什么?

【问题讨论】:

    标签: android performance kotlin reflection


    【解决方案1】:

    其实还有第三种可能(我猜……):

    shoppingCartViewModel.productList.observe(viewLifecycleOwner, shoppingCartAdapter::submitList)
    

    关于性能差异,请参见 question,但简而言之,差异可以忽略不计。您应该更关注哪个版本更具可读性。我肯定会尽量避免第一种方法,这让我很困惑。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-19
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2019-11-15
      • 2011-05-01
      相关资源
      最近更新 更多