Java – TheItem loadInitial method is not called in PositionalDataSource

The loadInitial method is not called in PositionalDataSource… here is a solution to the problem.

The loadInitial method is not called in PositionalDataSource

I implemented PositionalDataSource

from the Paging library in Java and ran into a problem where the constructor of the subclass of PositionalDataSource was called, but the loadInitial method was not called afterwards.

public HistoryPositionalDataSource(List<CallTable> callLogs)
{
    this.callLogs = callLogs;
    Log.d("PaginationDataSource", "Constructor");
}

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
    Log.d("PaginationDataSource", "loadInitial");
    if (callLogs!=null && !callLogs.isEmpty())
    {
        int totalCount = computeCount();
        int position = computeInitialLoadPosition(params, totalCount);
        int loadSize = computeInitialLoadSize(params, position, totalCount);
        callback.onResult(loadRangeInternal(position, loadSize), position, totalCount);
    }
}

@Override
public void loadRange(@NonNull LoadRangeParams params, @NonNull LoadRangeCallback callback) {
    callback.onResult(loadRangeInternal(params.startPosition, params.loadSize));
}

This is my PageListConfig

private void init() {
    pagedListConfig = (new PagedList.Config.Builder()).setEnablePlaceholders(true)
            .setInitialLoadSizeHint(Integer.MAX_VALUE).setPageSize(Integer.MAX_VALUE).build();
    Executor executor = Executors.newFixedThreadPool(3);
    List<CallTable> listLogs = getCallLogs(context);
    historyDataSourceFactory = new HistoryDataSourceFactory(listLogs);
    LivePagedListBuilder livePagedListBuilder = new LivePagedListBuilder(historyDataSourceFactory, pagedListConfig);
    pagedCallLogs =  livePagedListBuilder
            .setFetchExecutor(executor)
            .build();
}

Factory Class:

public class HistoryDataSourceFactory extends DataSource.Factory {

private static final String TAG = HistoryDataSourceFactory.class.getSimpleName();
private HistoryPositionalDataSource historyPositionalDataSource;

public HistoryDataSourceFactory(List<CallTable> callLogs)
{
    if (callLogs!=null && !callLogs.isEmpty())
    {
        Log.d("PaginationFactory", "NotNullLogs");
        historyPositionalDataSource = new HistoryPositionalDataSource(callLogs);
    }
}

@Override
public DataSource create() {
    return historyPositionalDataSource;
}
}

My getPagedCallLogs method:

public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {

if (pagedCallLogs!=null && pagedCallLogs.getValue()!=null)
    {
        Log.d("PagingGetData", "Done");
        return pagedCallLogs;
    }
    else
    {
        Log.d("PagingGetData", "Null");
        return null;
    }
}

The log image is as follows.
Logs

Solution

Set the load size and offset
With PagedList.Config, you don’t have to calculate the load range yourself.

Change your loadInitial functionality

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {
    Log.d("PaginationDataSource", "loadInitial");
    if (callLogs!=null && !callLogs.isEmpty())
    {
        callback.onResult(loadRangeInternal(0, params.requestedLoadSize), 0);
    }
}

Edit:
Try this configuration as well

 PagedList.Config config =
            new PagedList.Config.Builder()
                    .setPageSize(50)
                    .setEnablePlaceholders(false)
                    .setPrefetchDistance(25)
                    .build();

Edit 2:
Try changing the extension from DataSource.Factory to DataSource.Factory< Integer, ModelClass> and PositionalDataSource to PositionalDataSource< ModelClass>

Related Problems and Solutions