I'm trying to use the new Paging Library
and Room
as database but I'm facing a problem, the PagedList
returned by the database is not supposed to be the same list sent to the UI, I map
some entities before showing it to the user and during this map
operation I change the list size (add items), apparently the Paging Library
doesn't support this kind of operation because when I try to run the app I get this exception:
Caused by: java.lang.IllegalStateException: Invalid Function 'function_name' changed return size. This is not supported.
Looking at the paging library source code you see this method:
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;
}
Is there a workaround or something for dealing with when you add dynamic items to the PagedList
before using it?
This is what I'm doing
DAO
@Query("SELECT * FROM table_name")
fun getItems(): DataSource.Factory<Int, Item>
LocalSource
fun getItems(): DataSource.Factory<Int, Item> {
return database.dao().getItems()
.mapByPage { map(it) } // This map operation changes the list size
}