简体   繁体   English

DataSource.Factory 未使用正确的变量值 Android 分页

[英]DataSource.Factory is not using correct variable value Android Paging

I have a DataSource.Factory class "CategoriesDataSourceFactory".我有一个 DataSource.Factory class“CategoriesDataSourceFactory”。 I am passing a string variable "keyword" to class.我将字符串变量“关键字”传递给 class。 The create method of the class is creating a DataSource object using that keyword. class 的创建方法是使用该关键字创建数据源 object。 Problem is that when CategoriesDataSourceFactory is initialized for the first time with some value to the keyword.问题是当 CategoriesDataSourceFactory 第一次用关键字的一些值初始化时。 That value is not changing, no matter how many times a new instance of CategoriesDataSourceFactory is created with different values of keywords.无论使用不同的关键字值创建 CategoriesDataSourceFactory 的新实例多少次,该值都不会改变。

public class CategoriesDataSourceFactory extends DataSource.Factory<Long, CategoryModel> {
private CategoriesDataSource categoriesDataSource;
private MutableLiveData<CategoriesDataSource> categoriesDataSourceMutableLiveData;
String keyWord;
public CategoriesDataSourceFactory(String keyWord) {
    this.keyWord =keyWord;
    this.categoriesDataSourceMutableLiveData = new MutableLiveData<>();
}

@NonNull
@Override
public DataSource<Long, CategoryModel> create() {
    Log.i("KeyWord",keyWord!=null?keyWord:"Keyword Null"); //it is showing value of first time the object of this class is initilized.

    categoriesDataSource = new CategoriesDataSource(keyWord);
    categoriesDataSourceMutableLiveData.postValue(categoriesDataSource);
    return categoriesDataSource;
}

I am initilizaing it from a viewmodel我正在从视图模型中初始化它

  public class CategoriesFragmentViewModel extends ViewModel {
    // TODO: Implement the ViewModel

    LiveData<PagedList<CategoryModel>> categoryListLiveData;
    CategoriesDataSourceFactory categoriesDataSourceFactory;
    public CategoriesFragmentViewModel() {

       init("first time");
    }

// I am calling this method multiple times with  different keyword value 
    public void init(String keyWord) {
        Log.i("KeyWordViewmodel",keyWord!=null?keyWord:"Keyword Null");
        categoriesDataSourceFactory = new CategoriesDataSourceFactory(keyWord);

        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(true)
                .setInitialLoadSizeHint(10)
                .setPageSize(10)
                .setPrefetchDistance(4)
                .build();

        categoryListLiveData = new LivePagedListBuilder<Long,CategoryModel>(categoriesDataSourceFactory,config).build();
    }

It looks like instead of posting a new PagedList to categoryListLiveData so that existing observers can receive new values when keyWord changes you are actually building a whole new LiveData instead.看起来不是将新的PagedList发布到categoryListLiveData以便现有观察者可以在keyWord更改时接收新值,而是实际上是在构建一个全新的LiveData

Consider instead using Transformations.switchMap on a LiveData<String> of search terms that then map to LiveData<PagedList> so that you don't drop your observers on the floor and potentially miss leaks.考虑改为在搜索词的LiveData<String>上使用Transformations.switchMap ,然后将 map 转换为LiveData<PagedList> ,这样您就不会将观察者放在地板上并可能错过泄漏。

Transformations.switchMap(keyWordLiveData, keyWord -> {
  ...
  LivePagedListBuilder<..>(...).build()
});

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

相关问题 Android 分页库 - Map 房间 DataSource.Factory&lt;*, DatabaseModel&gt; 到 DataSource.Factory&lt;*, PresenterModel&gt; - Android Paging Library - Map Room DataSource.Factory<*, DatabaseModel> to DataSource.Factory<*, PresenterModel> Android体系结构组件分页DataSource.Factory错误 - Android architecture components paging DataSource.Factory error 无法从Android的分页中的DataSource.Factory获得toLiveData - toLiveData not available from DataSource.Factory in Android's Paging Android Room 在使用 DataSource.Factory 时如何按文本搜索? - Android Room how to search by text when using DataSource.Factory? 自定义构造函数PageKeyedDataSource()在分页库的datasource.factory()中崩溃应用程序 - Custom constructor PageKeyedDataSource() crashes app in datasource.factory() of paging library 用于多个数据源的分页库 DataSource.Factory - Paging library DataSource.Factory for multiple data sources 如何从 DataSource.Factory 获取数据 - How to get data from DataSource.Factory 具有多视图 RecyclerView 的 Room DataSource.Factory - Room DataSource.Factory with multiple view RecyclerView 房间本地单元测试 - 从 DataSource.Factory 查询 PagedList - Room Local Unit Test - Query PagedList from DataSource.Factory 如何测试返回DataSource.Factory的Dao方法? - How to test Dao methods which return DataSource.Factory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM