简体   繁体   English

Android体系结构组件:如何通过ViewModel观察存储库中的LiveData

[英]Android Architecture Components: How is LiveData in the repository observed by a ViewModel

i'm studying the Android Architecture Components and i'm a little bit confused. 我正在研究Android体系结构组件 ,我有点困惑。 In the sample they use a repository and state that changes within the datasource of the repository are observed by the ViewModels. 示例中,他们使用存储库,并声明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. 我不明白如何将数据源中的更改推送到ViewModels,因为我看不到ViewModels中将它们订阅到存储库的任何代码。 Analogously, the fragments observe the ViewModel's LiveData, but they actually subscribe to the LiveData: 类似地,这些片段观察ViewModel的LiveData,但它们实际上订阅了LiveData:

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

I cannot see any kind of subscribing within the ViewModels to observe the Repository. 我看不到在ViewModels中进行任何形式的订阅来观察存储库。 Am i missing something? 我想念什么吗?

ViewModel is not observing any data it just returning LiveData object of Product so you can observe the data in ProductFragment ViewModel不会观察任何数据 ,只是返回Product的LiveData对象,因此您可以观察ProductFragment中的数据

This is how LiveData is observed in ProductFragment , In which the getObservableProduct() method called on ViewModel which returns LiveData<ProductEntity> 这是在ProductFragment中 观察 LiveData的方式,其中在ViewModel上调用的getObservableProduct()方法返回LiveData<ProductEntity>

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

This method in ViewModel called from ProductFragment ViewModel中的此方法从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 在该ProductViewModel的构造函数中,成员变量mObservableProduct的初始化如下,该变量从存储库中获取LiveData<ProductEntity>

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

If you dig deeper, in DataRepository , LiveData<ProductEntity> is fetched from DAO 如果您进行更深入的研究,则在DataRepository中将从DAO中获取LiveData<ProductEntity>

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

And in DAO its nothing but SQL query which returns the LiveData<ProductEntity> which is implemented by RoomCompiler . 在DAO中,它只不过是SQL查询 ,该查询返回由RoomCompiler实现的LiveData<ProductEntity> As you can see DAO using @Dao annotation which used by annotation processor and Write Dao implementation in ProductDao_Impl class. 如您所见,使用@Dao批注的DAO由批注处理器和ProductDao_Impl类中的Write Dao实现使用。

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

So In a nutshell , ViewModel holding References to all the data required by Activity or Fragment. 简而言之ViewModel保留对Activity或Fragment所需的所有数据的引用 Data get initialized in ViewModel and it can survive Activity configuration changes . 数据在ViewModel中初始化,并且可以在Activity 配置更改后保留下来。 Thus we are storing its references in ViewModel. 因此,我们将其引用存储在ViewModel中。 In our case LiveData is just wrapper around our object which is returned by DAO implementation as an Observable object. 在我们的例子中,LiveData只是包装我们的对象,而DAO实现将其作为Observable对象返回。 So we can observe this in any Activity or Fragment . 因此,我们可以在任何Activity或Fragment中观察到这一点。 So as soon as the data is changed at Data Source, it called postValue() method on LiveData and we get the callback 因此,一旦在数据源处更改了数据,它postValue()在LiveData上调用postValue()方法,并得到回调

Flow of LiveData DAO -> Repository -> ViewModel -> Fragment LiveData DAO的 流程 ->存储库-> ViewModel->片段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android架构组件ViewModel可以从多个LiveData返回模型组成一个对象吗? - Can an Android Architecture Components ViewModel compose an object from multiple LiveData returning models? 使用 Android 架构组件 LiveData &amp; ViewModel 时是否需要使用 onSaveInstanceState 和 onRestoreInstanceState? - Is there any need to use onSaveInstanceState and onRestoreInstanceState when using Android Architecture Components LiveData & ViewModel? Android Architecture Components如何结合数据库和网络中的LiveData? - Android Architecture Components how to combine LiveData from both database and network? 如何在Android体系结构组件中使用RxJava代替LiveData? - How to use RxJava instead of LiveData with android architecture components? Android架构组件,Android绑定和LiveData - Android Architecture Components, android binding and LiveData 如何在Android体系结构组件中将存储库注入到拦截器中? - How to inject repository to the interceptor in android architecture components? 如何在Android体系结构组件中的存储库中访问SharedPrefernces - How to access SharedPrefernces in Repository in Android architecture components Android架构组件ViewModel上下文 - Android Architecture Components ViewModel Context Android架构组件:绑定到ViewModel - Android Architecture Components: bind to ViewModel ViewModel中的LiveData如何使用转换观察存储库中的Livedata? - How can LiveData in ViewModel observe the Livedata in Repository using Transformations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM