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

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

问题描述

我正在研究 Android体系结构组件,我我有点困惑.在样本中,他们使用存储库并声明在其中变化的状态仓库的数据源由ViewModels观察.我不明白如何将数据源中的更改推送到ViewModels,因为我看不到ViewModels中将它们订阅到存储库的任何代码.类似地,这些片段观察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);
        }
    });

我看不到ViewModels中有任何类型的订阅来观察存储库.我想念什么吗?

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

推荐答案

ViewModel未观察到任何数据,它只是返回Product的LiveData对象,因此您可以观察

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

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中的此方法从

This method in ViewModel called from ProductFragment

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

在该

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);

如果您进行更深入的研究,请在

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<ProductEntity>.如您所见,使用@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对象返回.因此,我们可以在任何活动或片段中进行观察.因此,一旦在数据源处更改了数据,它就会在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

流量的LiveData DAO->存储库-> ViewModel->片段

Flow of LiveData DAO -> Repository -> ViewModel -> Fragment

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

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