Android 架构组件:ViewModel 如何观察存储库中的 LiveData [英] Android Architecture Components: How is LiveData in the repository observed by a ViewModel

查看:41
本文介绍了Android 架构组件:ViewModel 如何观察存储库中的 LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Android 架构组件 并且我'我有点困惑.在示例中,他们使用了一个存储库并在存储库的数据源由 ViewModel 观察.我不明白如何将数据源中的更改推送到 ViewModel,因为我在 ViewModel 中看不到任何将它们订阅到存储库的代码.类似地,片段观察 ViewModel 的 LiveData,但它们实际上订阅了 LiveData:

i'm studying the Android Architecture Components and i'm a little bit confused. In the sample they use a repository and state that changes within the datasource of the repository are observed by the ViewModels. I don't understand how the changes within the datasource are pushed to the ViewModels, as i cannot see any code within the ViewModels that subscribes them to the repository. Analogously, the fragments observe the ViewModel's LiveData, but they actually subscribe to the LiveData:

 // Observe product data
    model.getObservableProduct().observe(this, new Observer<ProductEntity>() {
        @Override
        public void onChanged(@Nullable ProductEntity productEntity) {
            model.setProduct(productEntity);
        }
    });

我在 ViewModel 中看不到任何订阅来观察存储库.我错过了什么吗?

I cannot see any kind of subscribing within the ViewModels to observe the Repository. Am i missing something?

推荐答案

ViewModel 不观察任何数据它只是返回 Product 的 LiveData 对象,以便您可以观察 ProductFragment

ViewModel is not observing any data it just returning LiveData object of Product so you can observe the data in ProductFragment

这就是 LiveDataProductFragment,其中调用了getObservableProduct()方法在返回 LiveData

This is how LiveData is observed in ProductFragment, In which the getObservableProduct() method called on ViewModel which returns LiveData<ProductEntity>

// Observe product data
    model.getObservableProduct().observe(this, new Observer<ProductEntity>() {
        @Override
        public void onChanged(@Nullable ProductEntity productEntity) {
            model.setProduct(productEntity);
        }
    });

ViewModel 中的这个方法从 ProductFragment

This method in ViewModel called from ProductFragment

public LiveData<ProductEntity> getObservableProduct() {
    return mObservableProduct;
}

ProductViewModel 成员变量 mObservableProduct 初始化如下,获取LiveData from Repository

In constructor of that ProductViewModel the member variable mObservableProduct is initialized as follows, Which get LiveData<ProductEntity> from Repository

private final LiveData<ProductEntity> mObservableProduct;
mObservableProduct = repository.loadProduct(mProductId);

如果你深入挖掘,在 DataRepositoryLiveData从 DAO

If you dig deeper, in DataRepository, LiveData<ProductEntity> is fetched from DAO

public LiveData<ProductEntity> loadProduct(final int productId) {
    return mDatabase.productDao().loadProduct(productId);
}

在 DAO 中,它只是 SQL 查询,它返回由 RoomCompiler 实现的 LiveData.如您所见,DAO 使用 @Dao 注释,注释处理器使用了注释处理器和 ProductDao_Impl 类中的 Write Dao 实现.

And in DAO its nothing but SQL query which returns the LiveData<ProductEntity> which is implemented by RoomCompiler. As you can see DAO using @Dao annotation which used by annotation processor and Write Dao implementation in ProductDao_Impl class.

@Query("select * from products where id = :productId")
LiveData<ProductEntity> loadProduct(int productId);

所以,简而言之ViewModel 持有对 Activity 或 Fragment 所需的所有数据的引用.数据在 ViewModel 中被初始化,它可以在 Activity 配置更改中存活下来.因此,我们将其引用存储在 ViewModel 中.在我们的例子中,LiveData 只是我们对象的包装器,它由 DAO 实现作为 Observable 对象返回.所以我们可以在任何Activity 或 Fragment 中观察到这一点.因此,一旦数据源中的数据发生更改,它就会调用 LiveData 上的 postValue() 方法,我们得到 回调

So In a nutshell, ViewModel holding References to all the data required by Activity or Fragment. Data get initialized in ViewModel and it can survive Activity configuration changes. Thus we are storing its references in ViewModel. In our case LiveData is just wrapper around our object which is returned by DAO implementation as an Observable object. So we can observe this in any Activity or Fragment. So as soon as the data is changed at Data Source, it called postValue() method on LiveData and we get the callback

流程DAO -> Repository -> ViewModel -> Fragment

这篇关于Android 架构组件:ViewModel 如何观察存储库中的 LiveData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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