How to Combine Two PagingSource Queries into a Single PagingSource in Room?

I’m currently working on an Android project using Room for managing a large database with billions of records. I have two entities, Folder and File, each stored in separate tables. I’m using Room’s PagingSource to paginate these results, and I can retrieve them separately using the following queries:

@Query("""
    SELECT * FROM Folder 
    WHERE parentFolderId = :parentFolderId 
    ORDER BY 
    CASE 
        WHEN :sortBy = 'NAME' THEN name 
        WHEN :sortBy = 'TIME' THEN creationTime 
        WHEN :sortBy = 'SIZE' THEN size 
    END
""")
fun getFolders(parentFolderId: Long, sortBy: SortBy): PagingSource<Long, Folder>

@Query("""
    SELECT * FROM File 
    WHERE folderId = :parentFolderId 
    ORDER BY 
    CASE 
        WHEN :sortBy = 'NAME' THEN name 
        WHEN :sortBy = 'TIME' THEN creationTime 
        WHEN :sortBy = 'SIZE' THEN size
    END
""")
fun getFiles(parentFolderId: Long, sortBy: SortBy): PagingSource<Long, File>

Here are the Entities

@Serializable
@Entity(
    foreignKeys = [
        ForeignKey(
            entity = Folder::class,
            parentColumns = ["folderId"],
            childColumns = ["parentFolderId"],
            onDelete = ForeignKey.CASCADE
        ),
        ForeignKey(
            entity = Folder::class,
            parentColumns = ["folderId"],
            childColumns = ["folderId"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["folderId"]), Index(value = ["parentFolderId"])],
    primaryKeys = ["folderId", "parentFolderId"]
)
data class Folder(
    val folderId: Long = UUID.randomUUID().mostSignificantBits and Long.MAX_VALUE,
    val parentFolderId: Long? = null,
    val name: String,
    val size: Long,
    val userTag: UserTag? = null,
    val creationTime: Long,
    val lastOpenedTime: Long? = null,
    val userId: String
)

@Serializable
@Entity(
    foreignKeys = [
        ForeignKey(
            entity = Folder::class,
            parentColumns = ["folderId"],
            childColumns = ["folderId"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["fileId"]), Index(value = ["folderId"])],
    primaryKeys = ["fileId", "folderId"]
)
data class File(
    val fileId: Long = UUID.randomUUID().mostSignificantBits and Long.MAX_VALUE,
    val name: String,
    val size: Long,
    val fileType: FileType,
    val userTag: UserTag? = null,
    val creationTime: Long,
    val lastOpenedTime: Long? = null,
    val userId: String,
    val folderId: Long? = null,
    val chunkCount: Int,
    val fileHash: String
)

Problem:

These queries return Folder and File results separately as PagingSource. However, I need to combine these results into a single PagingSource and sort them together based on the selected criteria (e.g., name, creationTime, size).

Question:

How can I merge these two PagingSource queries into a single PagingSource so that I can paginate the combined results in Room? Is there an efficient way to handle this in Room, considering the scale of the data?

Any guidance or examples would be greatly appreciated!

Thanks in advance for your help!

You should solve this in your sql query. Sqlite has a compound select statements which allows you to combine rows from different tables. You just have to be careful with the columns, as all select statements have to return same number of columns and columns at the same position must have compatible types – it is best to hand pick the columns instead of using *. The resulting query can look like this:

SELECT
1 AS isFolder, -- for identification
NULL AS fileHash, -- add null column for data that are only in one table
name,
creationTime,
size
FROM Folder 
WHERE parentFolderId = :parentFolderId

UNION

SELECT
0 AS isFolder,
fileHash,
name,
creationTime,
size
FROM File 
WHERE folderId = :parentFolderId

ORDER BY ...

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật