1 year ago

#144156

test-img

Eric Li

Room relational query method with paging

In Room 2.4, there is a new feature called relational query method in DAO which you can write your custom query to select columns from 2 entities and Room can be able to aggregate into Map<TableA, List<TableB>> return type.

I have a fairly complicated query which do left join with nested queries to return a map of railway stations and their associated rail lines (many-to-many relationship). I tried to make a @Query method returns Flow<Map<RailStation, List<RailLine>>> and it can return the map that I want.

Now, I want to go one step further to make it returns paging 3's PagingSource. As the original type is a Map, so I think I should make the paging @Query method as PagingSource<Int, Map.Entry<RailStation, List<RailLine>>>. (Map.Entry should be the representative type of a single list item rather than Map as it represent the whole query result.) However, the Room annotation processor complainted about this line saying that it cannot handle this type:

[ksp] RailStationDao.kt:130: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer, java.util.Map.Entry<RailStation, java.util.List<RailLine>>>).

So my question is: does the Room annotation processor and Paging 3 support for relational query method with paging 3? If not, is there any alternative way to archive the same goal? It seems like the @Relation annotation in Room can only support for simple table joining, but my case is I need to write nested query in the LEFT JOIN clause.

android-room

android-room-relation

0 Answers

Your Answer

Accepted video resources