简体   繁体   English

带 LiveData 的 ViewModel 与不带 LiveData 的 ViewModel 有何不同?

[英]Different between ViewModel with LiveData vs ViewModel Without LiveData?

I read many tutorial about viewmodel and livedata but i do not get actually use of livedata within viewmodel class in mvvm pattern.我阅读了许多关于 viewmodel 和 livedata 的教程,但我没有在 mvvm 模式中的 viewmodel 类中实际使用 livedata。 thanks in advance.提前致谢。

The MVVM pattern is to only talk down V -> VM -> M and react up M -> VM -> V. Meaning the View can call methods on the ViewModel but the ViewModel doesn't have a reference to the View to call methods on it (setting data). MVVM模式是只讲V-> VM-> M并作出反应M-> VM->V。这意味着View可以调用ViewModel上的方法,但是ViewModel没有对View调用方法的引用在上面(设置数据)。 The way to communicate from the ViewModel to the View is by the View observing some variable (ObservableField, LiveData, RxJava, etc). 从ViewModel到View的通信方式是通过View观察一些变量(ObservableField,LiveData,RxJava等)。

LiveData is a great observable object to provide communication between the viewModel and the View especially over state changes like rotation. LiveData是一个很好的可观察对象,可提供viewModel和View之间的通信,尤其是在状态变化(例如旋转)时。 It also provides great communication between the Model and the View in regards to database changes and Room. 它还在模型和视图之间提供有关数据库更改和Room的良好通信。

As per Google Docs, If you are already using a library like Rx or Agera, you can continue using them instead of LiveData. 根据Google文档,如果您已经在使用Rx或Agera之类的库,则可以继续使用它们而不是LiveData。 But in this case, it is your responsibility to handle object allocation and de-allocation per Android components life cycle. 但是在这种情况下,您有责任按照Android组件生命周期处理对象分配和取消分配。

When working with MVVM pattern,since viewmodel has no reference to view, you'll need observable data holder to observe changes so you can properly update your view. 在使用MVVM模式时,由于viewmodel没有对视图的引用,因此需要可观察的数据持有者来观察更改,以便可以正确更新视图。

In this case you can use livedata,one of architecture components or any other observable like from Rx. 在这种情况下,您可以使用实时数据,体系结构组件之一或任何其他可观察到的数据,例如Rx。

The main difference is livedata respects android lifecycle and rx observables don't. 主要区别是livedata尊重android的生命周期,而rx observables则没有。

With livedata,there would be no crash due to stopped activity when onChange called since it's lifecycle aware. 使用livedata时,不会因onChange调用而停止活动而导致崩溃,因为onChange是生命周期感知的。 But without livedata,it is up to you to handle this case. 但是,如果没有livedata,则由您来处理这种情况。

Android dev commonly use ViewModel as a container of LiveData like this Android开发人员通常使用ViewModel这样的LiveData容器

class MyViewModel {
    val myLiveData = MutableLiveData<String>()
}

But, why don't we use the String class directly? 但是,为什么不直接使用String类呢?

class MyViewModel {
    val myString = "Hello"
}

Because we want to use Observer pattern to the String. 因为我们要对字符串使用Observer模式。

model.myLiveData.postValue("Echo")

model.myLiveData.observe(this, Observer {
    // Show "Echo"
    toast(it)
})

Why do we need ViewModel then? 那么为什么我们需要ViewModel? Because we want to get the same ViewModel instance (singleton) on our Activity or Fragment. 因为我们要在Activity或Fragment上获得相同的ViewModel实例(单例)。

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    viewModel = ViewModelProviders.of(this)[MyViewModel::class.java]
    viewModel.myLiveData.observe(this, Observer {
        textView.text = it
    })

I think that should be enough to start seeing the difference between ViewModel and LiveData. 我认为应该足以开始看到ViewModel和LiveData之间的区别。

Different between ViewModel with LiveData vs ViewModel Without LiveData? 带LiveData的ViewModel与不带LiveData的ViewModel有何区别?

That's up to you, do you need a LiveData on a ViewModel or not? 这取决于您,是否需要在ViewModel上使用LiveData?

i do not get actually use of livedata within viewmodel class in mvvm pattern. 我没有在mvvm模式的viewmodel类中实际使用livedata。

After understanding the difference between ViewModel and LiveData, please read more about the MVVM pattern. 了解ViewModel和LiveData之间的区别之后,请阅读有关MVVM模式的更多信息。

The View Model in Model-View-View-Model is not the same as Jetpack's ViewModel and LiveData. Model-View-View-Model中的View Model与Jetpack的ViewModel和LiveData不同。 View Model in MVVM is a concept, you can create your own View Model using normal Java class. MVVM中的视图模型是一个概念,您可以使用常规Java类创建自己的视图模型。 As long as it conforms to the MVVM behavior. 只要符合MVVM行为即可。

In simple format, livedata is the magical observable data holder class, which is lifecycle aware also.在简单的格式中, livedata是神奇的可观察数据持有者类,它也是生命周期感知的。

If you use livedata inside viewmodel :如果您在viewmodel使用livedata

  1. you can setValue or postValue anything to the livedata and your UI will be able to observe the value and it's changes and show in the UI.你可以setValuepostValue什么的livedata和您的用户界面将能够观察UI中的价值和它的变化和表演。
  2. you can use dataBinding for much better approach to observe the livedata .您可以使用dataBinding为更好的方法来观察livedata
  3. no manual configuration needed to achieve memory leak.无需手动配置即可实现内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM