[英]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.