如何从存储库返回 LiveData

编程入门 行业动态 更新时间:2024-10-21 15:45:37
本文介绍了如何从存储库返回 LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我只是看不到如何将 LiveData 从 Repo 链接到 VM,所以我尝试将其归结为最简单的示例!:

I just can't see how to chain LiveData from Repo to VM, so I have tried to boil this down to the most simple of example!:

片段

class LoginFragment : Fragment() { private lateinit var loginViewModel: LoginViewModel private var mCurrentName = "Blank!" override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { // Inflate the layout for this fragment val binding: LoginFragmentBinding = DataBindingUtil.inflate( inflater, R.layout.login_fragment, container, false) binding.apply { loginButton.setOnClickListener{ loginViewModel.changeText() } } return binding.root } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) loginViewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java) loginViewModel.getCurrentName().observe(viewLifecycleOwner, Observer { mCurrentName = it // I'm expecting mCurrentName to equal "Button Clicked!" when button clicked.. makeToast() // Toast works, but variable remains unchanged.. }) } private fun makeToast() { Toast.makeText(activity, mCurrentName, Toast.LENGTH_LONG).show() }

视图模型

class LoginViewModel : ViewModel() { private val firestoreRepository : FirestoreRepository = FirestoreRepository() private var mCurrentName = MutableLiveData<String>() fun changeText(){ mCurrentName = firestoreRepository.changeText() }

存储库

class FirestoreRepository { private val mCurrentName = MutableLiveData<String>() fun changeText(): MutableLiveData<String> { mCurrentName.value = "Button Clicked!!" return mCurrentName }

我假设我误解了观察者函数的工作原理..

I'm assuming I have misunderstood how the observer function works..

推荐答案

其实如果你在repository中维护LiveData,我认为不需要单独的LiveData 在 ViewModel 中也是如此.您只需要从活动中观察一次 LiveData.然后直接对存储库实例进行任何更改.所以如果我必须在你的代码中展示它,它可能看起来像这样.

Actually If you are maintaining LiveData in repository, I don't think there's a need of separate LiveData in ViewModel as well. You just have to observe the LiveData once from the activity. Then make any changes to repository instance directly. So If I have to show it in your code, It might look something like this.

  • Activity 类:将 makeToast 方法更改为observeCurrentName(),如下所示:

  • Activity class: Change makeToast method to observeCurrentName() like this: private fun observeCurrentName() { vm.getCurrentName().observe(this, Observer{ //Toast here }) }

  • 你的虚拟机:

  • Your VM:

    class LoginViewModel : ViewModel() { ... fun getCurrentName(): MutableLiveData<String>{ return repository.getCurrentName() } fun setCurrentName(name: String?){ repository.setCurrentName(name) } ... }

  • 您的存储库:

  • Your repository:

    class FirestoreRepository { private val mCurrentName = MutableLiveData<String>() fun getCurrentName(): MutableLiveData<String>{ return mCurrentName } fun setCurrentName(name: String?){ mCurrentName.value = name //This will trigger observer in Activity } }

  • 更多推荐

    如何从存储库返回 LiveData

    本文发布于:2023-11-28 16:34:03,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1643150.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:LiveData

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!