使用 viewLifecycleOwner 作为 LifecycleOwner [英] Use viewLifecycleOwner as the LifecycleOwner

查看:108
本文介绍了使用 viewLifecycleOwner 作为 LifecycleOwner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段:

class MyFragment : BaseFragment() {

   // my StudentsViewModel instance
   lateinit var viewModel: StudentsViewModel

   override fun onCreateView(...){
        ...
   }

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
       super.onViewCreated(view, savedInstanceState)

       viewModel = ViewModelProviders.of(this).get(StudentsViewModel::class.java)
       updateStudentList()
   }

   fun updateStudentList() {
        // Compiler error on 'this': Use viewLifecycleOwner as the LifecycleOwner
        viewModel.students.observe(this, Observer {
            //TODO: populate recycler view
        })
    }
}

在我的片段中,我有一个 StudentsViewModel 的实例,它在 onViewCreated(...) 中启动.

In my fragment, I have a instance of StudentsViewModel which is initiated in onViewCreated(...).

在,StudentsViewModelstudents是一个LiveData:

class StudentsViewModel : ViewModel() {
    val students = liveData(Dispatchers.IO) {
          ...
    }
}

回到 MyFragment,在函数 updateStudentList() 中,我收到编译器错误,抱怨我传入 .observe 的 this 参数(this, Observer{...}) that Use viewLifecycleOwner as the LifecycleOwner

Back to MyFragment, in function updateStudentList() I get compiler error complaining the this parameter I passed in to .observe(this, Observer{...}) that Use viewLifecycleOwner as the LifecycleOwner

为什么会出现这个错误?如何摆脱它?

Why I get this error? How to get rid of it?

推荐答案

为什么会出现这个错误?

Why I get this error?

Lint 建议您使用片段视图的生命周期 (viewLifecycleOwner) 而不是片段本身的生命周期 (this).Google 的 Ian Lake 和 Jeremy Woods 在 本次 Android 开发者峰会演示中讨论了差异,Ibrahim Yilmaz 介绍了这篇 Medium 帖子中的差异:

Lint is recommending that you use the lifecycle of the fragment's views (viewLifecycleOwner) rather than the lifecycle of the fragment itself (this). Ian Lake and Jeremy Woods of Google go over the difference as part of this Android Developer Summit presentation, and Ibrahim Yilmaz covers the differences in this Medium post In a nutshell:

  • viewLifecycleOwner 与片段何时拥有(和失去)其 UI(onCreateView()onDestroyView())相关联

  • viewLifecycleOwner is tied to when the fragment has (and loses) its UI (onCreateView(), onDestroyView())

this 与片段的整个生命周期(onCreate()onDestroy())相关联,可能会更长

this is tied to the fragment's overall lifecycle (onCreate(), onDestroy()), which may be substantially longer

如何摆脱它?

替换:

viewModel.students.observe(this, Observer {
        //TODO: populate recycler view
    })

与:

viewModel.students.observe(viewLifecycleOwner, Observer {
        //TODO: populate recycler view
    })

在你当前的代码中,如果onDestroyView()被调用,但onDestroy()没有被调用,你将继续观察LiveData,当您尝试填充不存在的 RecyclerView 时可能会崩溃.通过使用 viewLifecycleOwner,您可以避免这种风险.

In your current code, if onDestroyView() is called, but onDestroy() is not, you will continue observing the LiveData, perhaps crashing when you try populating a non-existent RecyclerView. By using viewLifecycleOwner, you avoid that risk.

这篇关于使用 viewLifecycleOwner 作为 LifecycleOwner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆