使用MutableLiveData<String>而不是String的优点是什么?

huangapple go评论63阅读模式
英文:

Advantages of using MutableLiveData<String> over String?

问题

这是我的全局对象:

object FcmData {
    val type: String by lazy {
        String()
    }
    val type2: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }
}

我正在使用 type 值来持久化一个可以被我的服务和片段更新和访问的值。

在我的用例中,应用这两种类型是否有任何区别,如果有的话?

英文:

Here is my global object:

object FcmData {
    val type: String by lazy {
        String()
    }
    val type2: MutableLiveData&lt;String&gt; by lazy {
        MutableLiveData&lt;String&gt;()
    }
}

I'm using the type value to persist a value that can be updated and accessed by my service and fragment.

What is the difference, if any, when applying the two types in my use-case?

答案1

得分: 2

LiveData 是一个可观察的数据持有类。因此,当您需要跟踪该值并根据其更改运行任何操作时(例如:数据绑定),您应该使用它。

因此,如果您只是读取/写入值而不进行观察,那么不应该使用 LiveData。

更多关于 LiveData

英文:

LiveData is an observable data holder class. So you should be using this when you need to track the value and run any actions according to its changes (ex: Databinding).

So if you are just reading/writing the value without observing - you should not be using LiveData

More about livedata

答案2

得分: 1

那些类型完全不同,很难找到它们之间的(不)优点。

String只是一个对象。实际上,您使用了delegate,因此通过Lazy委托访问该对象(当它被使用时初始化值)。

在第二种情况下,您使用MutableLiveDataString包装成对象(而MutableLiveData也被包装在Lazy委托中)。每当您更新该数据时,所有观察者都会收到通知。重要的是,这个类型来自Android库,并且在android-arch库中可用。

那么,这两者之间有什么区别呢?

这两个值都是不可变的。我的意思是这些字段没有setter方法。但是,您可以更改type2的内部值,因为MutableLiveData具有诸如updateValuepostValue之类的方法,因此它是可变值的不可变包装器。

type中,您只需懒惰地初始化值,而不能更改它。

如果您正在寻找除MutableLiveData之外的其他可能性,您可以查看kotlin std lib中提供的Observable委托。该委托可以与MutableLiveData进行比较(基本上做相同的事情)。文档可以在此处找到:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.properties/-delegates/observable.html

英文:

Those types are totally different and it's hard to find (dis)advantages between those two.

String is just an object. Actually, you use delegate, so access to that object is via Lazy delegate (value is initialized when it's used).

In the second case you use MutableLiveData which is wrapping the String into the object (and MutableLiveData is also wrapped in Lazy delegate). Whenever you update that data all observers will be notified. What is important that type comes from android library and it's available in android-arch library.

So, what's the difference between those two?

Both values are immutable. I mean there is no setter for those fields. But, you can change internal value of type2, because MutableLiveData has methods like updateValue and postValue, so it's immutable wrapper over mutable value.

In type you just initialize value lazily and you cannot change it.

If you look for some other possibilities than MutableLiveData you can look into Observable delegate that is available in kotlin std lib. That delegate can be compared to MutableLiveData (as does basically the same). The docs can be found here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.properties/-delegates/observable.html

huangapple
  • 本文由 发表于 2020年1月3日 19:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定