我正在尝试使用新的Paging Library
和Room
作为数据库,但我遇到了一个问题,PagedList
数据库返回的列表不应该与发送到 UI 的列表相同,我map
在向用户显示之前和在此map
操作期间有一些实体我更改了列表大小(添加项目),显然Paging Library
不支持这种操作,因为当我尝试运行应用程序时出现此异常:
Caused by: java.lang.IllegalStateException: Invalid Function 'function_name' changed return size. This is not supported.
查看分页库源代码,您会看到此方法:
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;
}
当您在使用它之前添加动态项目时,是否有解决方法或需要处理的东西PagedList
?
这就是我正在做的
道
@Query("SELECT * FROM table_name")
fun getItems(): DataSource.Factory<Int, Item>
本地资源
fun getItems(): DataSource.Factory<Int, Item> {
return database.dao().getItems()
.mapByPage { map(it) } // This map operation changes the list size
}