调用getValue()后,LiveData对象保持为空 [英] LiveData Object keeps being null after getValue() is called

查看:643
本文介绍了调用getValue()后,LiveData对象保持为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新LiveData对象上存储库内对象的成员变量.问题是,如果我调用了getValue()方法,尽管该值确实存在于我的RoomLibrary中,但我仍会收到NullPointerException.

I want to update a member variable of an object inside my Repository on a LiveData- Object. The problem is, that if I call the getValue() Method, I keep getting an NullPointerException, although the value does exist inside my Room- Library.

我现在的问题是,如何从LiveData对象获取值,而无需调用observe()方法? (我无法在我的存储库中调用观察方法,因为该方法要我输入LifeCycleOwner-引用,该引用在我的存储库中不存在.)

My question now is, how do I get the value from the LiveData Object without calling the observe() Method? (I am not able to call the observe method inside my repository, cause that method wants me to enter a LifeCycleOwner- reference, which is not present inside my repository).

有什么方法可以从LiveData对象中获取值?

Is there any way to get the value out of the LiveData- object?

我的架构如下所示: ViewModel->存储库-> Dao

My architecture looks like that: ViewModel --> Repository --> Dao

推荐答案

您需要先在ViewModel中初始化LiveData对象,然后再在Activity/Fragment中观察它,像这样

You need to initialize LiveData object in ViewModel before observing it in Activity/Fragment like this

ProductViewModel.java

public ProductViewModel(DataRepository repository, int productId) {
    mObservableProduct = repository.loadProduct(mProductId);
}
public LiveData<ProductEntity> getObservableProduct() {
    return mObservableProduct;
}

这里的observableProduct是用于观察产品详细信息的LiveData,它是在构造函数中初始化的,并使用getObservableProduct()方法获取

Here observableProduct is LiveData for observing product details which is initialized in constructor and fetched using getObservableProduct() method

然后您可以像这样在Activity/Fragment中观察LiveData

Then you can observe the LiveData in Activity/Fragment like this

MainActivity.java

productViewModel.getObservableProduct().observe(this, new Observer<ProductEntity>() {
    @Override
    public void onChanged(@Nullable ProductEntity productEntity) {
        mProduct = productEntity;
    }
});

由于您已经设置了代码架构,例如 LiveData的流程是

As you already setup your code architecture like Flow of LiveData is

DAO->储存库-> ViewModel->片段

您不需要在存储库中观察LiveData,因为您无法从那里更新UI.而是从活动"中观察它,然后从那里更新UI.

You don't need to observe LiveData in repository because you cannot update UI from there. Observe it from Activity instead and update UI from there.

正如您说的那样,在getValue()上给null,请确保您正在更新数据库并从DAO的单个实例中获取数据库,因为按照我与DAO的合作,它将不会通知数据库将一个DAO实例更新为第二个DAO实例. LiveData

As you are saying its giving null on getValue(), make sure you are updating db and fetching db from single instance of DAO as per I worked with DAO it will not notify db update of one DAO instance to 2nd DAO instance with LiveData

您还可以按照@Martin Ohlin的建议观察Forever,但它不是生命周期感知的,并且可能导致崩溃.永久观察之前先检查您的要求

Also you can observeForever as suggested by @Martin Ohlin, but it will not be lifecycle aware and may lead to crashes. Check your requirement before observing forever

请参阅完整的实时数据流

请参阅 DAO问题

编辑1-不使用LifecycleOwner

您可以使用void observeForever (Observer<T> observer)(

You can use void observeForever (Observer<T> observer) (reference) method to observe LiveData without providing any LifecycleOwner as I provided by using this context in above example.

这是您无需提供任何LifecycleOwner即可观察LiveData并在存储库本身中观察LiveData的方式

This is how you can observe LiveData without providing any LifecycleOwner and observe the LiveData in repository itself

private void observeForeverProducts() {
    mDatabase.productDao().loadAllProducts().observeForever(new Observer<List<ProductEntity>>() {
        @Override
        public void onChanged(@Nullable List<ProductEntity> productEntities) {
                Log.d(TAG, "onChanged: " + productEntities);
            }
        });
    }

但是您需要显式调用removeObserver(Observer)以停止观察LiveData,该数据在以前的情况下是通过LifecycleOwner自动完成的.因此,根据文档

But you need to call removeObserver(Observer) explicitly to stop observing the LiveData which was automatically done in previous case with LifecycleOwner. So as per documentation

您应该手动调用removeObserver(Observer)以停止观察此LiveData.尽管LiveData有这样的观察者之一,但它将被视为活动的.

You should manually call removeObserver(Observer) to stop observing this LiveData. While LiveData has one of such observers, it will be considered as active.

由于不需要LifecycleOwner,因此您可以在存储库中调用此函数,而无需使用您提到的 this 参数

As this doesn't require LifecycleOwner you can call this in Repository without using this parameter as you mentioned which is missing in your repository

这篇关于调用getValue()后,LiveData对象保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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