loadInitial method not getting called in PositionalDataSource<Item>

Feroz Khan :

I'm implementing PositionalDataSource from Paging Library in Java and getting an issue that constructor of PositionalDataSource's child class is getting called but after that loadInitial method is not getting called.

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));
}

Here's 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;
    }
}

Logs image is given below. Logs

Feroz Khan :

After too many struggle, I become able to resolve my issue. The issue was with my getPagedCallLog method. I wrote:

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;
}
}

I was taking Google I/O '18, in which he said that loadInitial is called by the pageList, then I realise that it wasn't getting called in my case. And it is working fine after removing pagedCallLogs.getValue()!=null which was my stupid mistake.

Now it looks like this:

public synchronized LiveData<PagedList<CallTable>> getPagedCallLogs() {
if (pagedCallLogs!=null)
{
    Log.d("PagingGetData", "Done");
    return pagedCallLogs;
}
else
{
    Log.d("PagingGetData", "Null");
    return null;
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=144625&siteId=1