Paging library DataSource.Factory for multiple data sources android
Asked Answered
G

0

5

I have multiple data sources. But there is only one DataSourceFactory. So, all sources is sharing one factory. I need one DataSourceFactory per data source.

In my app, I have multiple views with RecyclerViews and hence multiple custom data sources. So, do you end up creating multiple implementations of DataSource.Factory per data source or is there a more generic solution?

Edited

I found the solution in my case. In my previous version, I used only one Datasource Factory for all datasources. Now I create Datasource Factory Object whenever calling method from ViewModel like

private void initData(String id) {
    executor = Executors.newFixedThreadPool(5);

    factory = new EvaluationDataSourceFactory(compositeDisposable, api, id);

    networkState = Transformations.switchMap(factory.getDataSource(), source -> {
                Timber.d("network status get");
                return source.getNetworkState();
            }
    );

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setInitialLoadSizeHint(5)
                    .setPrefetchDistance(5)
                    .setPageSize(5).build();

    error = Transformations.switchMap(factory.getDataSource(), source -> {
        return source.getError();
    });

    assessments = (new LivePagedListBuilder(factory, pagedListConfig)).setFetchExecutor(executor).build();
}

public void getAssessments(String id) {
    initData(id);
}

Then, you can call this method from your activity or fragment like

viewModel.getAssessments(id);

It worked for me.

Glinys answered 19/2, 2019 at 14:40 Comment(1)
I already answered. Check my answer hereDoodlesack

© 2022 - 2024 — McMap. All rights reserved.