I have a DataSource.Factory class "CategoriesDataSourceFactory". I am passing a string variable "keyword" to class. The create method of the class is creating a DataSource object using that keyword. Problem is that when CategoriesDataSourceFactory is initialized for the first time with some value to the keyword. That value is not changing, no matter how many times a new instance of CategoriesDataSourceFactory is created with different values of keywords.
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();
}