Currently, we have a data structure stored in Room SQLite, named Todo
Our current workflow is as follow
- Dao is returning
DataSource.Factory<Integer, Todo> - Use
LivePagedListBuilderto turnDataSource.Factory<Integer, Todo>intoLiveData<PagedList<Todo>> - Observe
LiveData<PagedList<Todo>>, and usesubmitListto passPagedList<Todo>toPagedListAdapter<Todo, TodoAdapter.ViewHolder>
So far so good for such simple use case.
However, right now, we have a more complex UI requirement. We need to transform Todo to TransformedTodo, before submitList to PagedListAdapter.
Here's our transform function.
List<TransformedTodo> transform(Todo todo)
Note, it is possible to transform 1 Todo, to 1 or many TransformedTodo.
My initial planned workflow is
- Dao is returning
DataSource.Factory<Integer, Todo> - Use
DataSource.Factory.mapByPage, to transformDataSource.Factory<Integer, Todo>toDataSource.Factory<Integer, TransformedTodo> - Use
LivePagedListBuilderto turnDataSource.Factory<Integer, TransformedTodo>intoLiveData<PagedList<TransformedTodo>> - Observe
LiveData<PagedList<TransformedTodo>>, and usesubmitListto passPagedList<TransformedTodo>toPagedListAdapter<TransformedTodo, TodoAdapter.ViewHolder>
The tricky part is step 2.
If the transform function, is returning output List<TransformedTodo> where its size is different from input List<Todo>, exception will be thrown.
todosDataSourceFactory.mapByPage(new Function<List<Todo>, List<TransformedTodo>>() {
@Override
public List<TransformedTodo> apply(List<Todo> input) {
// Exception will be thrown if we are returning a List where its size is different from input.
}
});
The thrown exception is something which looks like
java.lang.IllegalStateException: Invalid Function com.yocto.wetodo.repository.TodoRepository$1@17b6f1b changed return size. This is not supported.
Here's why
// androidx.paging.DataSource
static <A, B> List<B> convert(Function<List<A>, List<B>> function, List<A> source) {
List<B> dest = function.apply(source);
if (dest.size() != source.size()) {
throw new IllegalStateException("Invalid Function " + function
+ " changed return size. This is not supported.");
}
return dest;
}
Seem like a limitation in paging library. Is there a way, to transform a PagedList to another PagedList with different size?
Reference links :
https://issuetracker.google.com/issues/142890117
How to use AAC paging library with list size different than list size returned by Room database