Paging library DataSource.Factory for multiple data sources android

我只是一个虾纸丫 提交于 2019-12-21 05:34:05

问题


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.

来源:https://stackoverflow.com/questions/54768784/paging-library-datasource-factory-for-multiple-data-sources-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!