0

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();
    }
sum20156
  • 646
  • 1
  • 7
  • 19

1 Answers1

1

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.

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.

Transformations.switchMap(keyWordLiveData, keyWord -> {
  ...
  LivePagedListBuilder<..>(...).build()
});
dlam
  • 3,547
  • 17
  • 20
  • Could you update your code sample with the updated code then? `categoryListLiveData = new LivePagedListBuilder(categoriesDataSourceFactory,config).build();` this line replaces your instance of `LiveData` completely instead of emitting a new value. – dlam Feb 19 '21 at 03:06