gpt4 book ai didi

android - 我应该在 ViewModel 中包含 LifecycleOwner 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:51:15 27 4
gpt4 key购买 nike

目前需要 LifecycleOwner 才能创建观察者。

我有在 ViewModel 中创建观察者的代码,因此在我的 Fragment 中检索 ViewModel 时我附加了 LifecycleOwner。

根据 Google 的文档。

警告:ViewModel 绝不能引用 View 、生命周期或任何可能包含对 Activity 上下文的引用的类。

我是否违反了该警告?如果违反了,您建议我以何种方式移动我创建的数据返回观察者?

我只做了一个观察者,所以我想知道它是否仍然有效。因为在谷歌的文档中它也说了。

ViewModel 对象可以包含 LifecycleObservers,例如 LiveData 对象。

主 fragment

private lateinit var model: MainViewModel

/**
* Observer for our ViewModel IpAddress LiveData value.
* @see Observer.onChanged
* */
private val ipObserver = Observer<String> {
textIp.text = it
hideProgressBar()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = ViewModelProviders.of(this).get(MainViewModel::class.java)
model.attach(this)
}

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? =
inflater?.inflate(R.layout.fragment_main, container, false)

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

buttonRetrieveIp.setOnClickListener {
showProgressBar()
model.fetchMyIp().observe(this, ipObserver) //Here we attach our ipObserver
}
}

override fun showProgressBar() {

textIp.visibility = View.GONE
progressBar.visibility = View.VISIBLE
}

override fun hideProgressBar() {

progressBar.visibility = View.GONE
textIp.visibility = View.VISIBLE
}

主视图模型

private var ipAddress = MutableLiveData<String>()
private lateinit var owner: LifecycleOwner

fun attach(fragment: MainFragment) {
owner = fragment
}

/**
* For more information regarding Fuel Request using Fuel Routing and Live Data Response.
* @see <a href="https://github.com/kittinunf/Fuel#routing-support">Fuel Routing Support</a>
* @see <a href="https://github.com/kittinunf/Fuel#livedata-support">Fuel LiveData Support</a>
* */
fun fetchMyIp(): LiveData<String> {

Fuel.request(IpAddressApi.MyIp())
.liveDataResponse()
.observe(owner, Observer {

if (it?.first?.statusCode == 200) {//If you want you can add a status code checker here.

it.second.success {

ipAddress.value = Ip.toIp(String(it))?.ip
}
}
})
return ipAddress
}

更新 1:根据@pskink 关于使用转换的建议改进了 ViewModel。

private lateinit var ipAddress:LiveData<String>

/**
* Improved ViewModel since January 23, 2018 credits to <a href="https://stackoverflow.com/users/2252830/pskink">pskink</a> <a href="
*
* For more information regarding Fuel Request using Fuel Routing and Live Data Response.
* @see <a href="https://github.com/kittinunf/Fuel#routing-support">Fuel Routing Support</a>
* @see <a href="https://github.com/kittinunf/Fuel#livedata-support">Fuel LiveData Support</a>
* */
fun fetchMyIp(): LiveData<String> {

ipAddress = Transformations.map(Fuel.request(IpAddressApi.MyIp()).liveDataResponse(), {

var ip:String? = ""

it.second.success {

ip = Ip.toIp(String(it))?.ip
}
ip
})

return ipAddress
}

最佳答案

没有。如果您希望观察 ViewModel 中某些 LiveData 的变化,您可以使用 observeForever(),它不需要 LifecycleOwner.

记得在 ViewModelonCleared() 事件中移除这个观察者:

val observer = new Observer() {
override public void onChanged(Integer integer) {
//Do something with "integer"
}
}

...

liveData.observeForever(observer);

...

override fun onCleared() {
liveData.removeObserver(observer)
super.onCleared()
}

很好的引用 observe LiveData 的例子.

关于android - 我应该在 ViewModel 中包含 LifecycleOwner 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396092/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com