Paging3:在Room DAO中使用PagingSource作为返回类型时,“不确定如何将游标转换为此方法的返回类型”

问题描述

我试图为新的Paging 3库模仿Google的代码实验室,当我尝试让Room DAO方法返回PagingSource时遇到以下错误:

D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:38: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
    public abstract androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser> getUserByUserName(@org.jetbrains.annotations.NotNull()
    
^D:\Programming\Android\something\app\build\tmp\kapt3\stubs\debug\com\someapp\something\data\db\UsersDao.java:43: error: Not sure how to convert a Cursor to this method's return type (androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser>).
public abstract androidx.paging.PagingSource<java.lang.Integer,com.someapp.something.data.db.GithubUser> getUserByNote(@org.jetbrains.annotations.NotNull()

这是我的UsersDao.kt

@Dao
interface UsersDao {

    @Insert
    fun insert(user: GithubUser): Completable

    @Insert
    fun insert(userList: List<GithubUser>): Completable

    @Query("DELETE FROM userDb")
    fun clearDb(): Completable

    @Query("SELECT * FROM userDb")
    fun getAllUsers(): Single<List<GithubUser>>

    @Query("SELECT EXISTS(SELECT 1 FROM userDb WHERE username LIKE :userName)")
    fun checkIfUserExists(userName: String): Boolean

    @Query("SELECT note FROM userDb WHERE username LIKE :userName")
    fun getNoteByUserName(userName: String): Single<String>

    @Query("SELECT * FROM userDb WHERE username LIKE :userName")
    fun getUserByUserName(userName: String): PagingSource<Int,GithubUser>

    @Query("SELECT * FROM userDb WHERE note LIKE :note")
    fun getUserByNote(note: String): PagingSource<Int,GithubUser>

}

我的GithubUser.kt看起来像这样:

@Entity(tableName = "userDb",indices = arrayOf(Index(value = ["username"],unique = true)))
class GithubUser (
    var username: String,var note: String,var url: String,var avatarUrl: String
) {
    @PrimaryKey(autoGenerate = true)
    var uid = 0
}

在用于分页Codelab的code中,DAO方法仅返回PagingSource,在Gradle或其他工具中没有额外的注释/魔术选项。我还查看了来自Github的其他示例,例如使用分页3库的thisthis,它们只是返回PagingSource而没有任何问题。谁能告诉我我错过了什么吗?

注意:在错误本身之前,我总是收到有关ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1的警告,但是该警告本身过去并没有引起任何问题,但是我只是在这里指出情况。

编辑:我使用以下Room / Paging lib版本:

    implementation "androidx.room:room-runtime:2.2.5"
    kapt "androidx.room:room-compiler:2.2.5"
    implementation 'androidx.room:room-rxjava2:2.2.5'

    implementation "androidx.paging:paging-runtime:3.0.0-alpha03"
    implementation 'androidx.paging:paging-rxjava2:3.0.0-alpha03'

解决方法

事实证明,您需要将会议室版本提高到2.3.0-alpha02或更高版本:

implementation "androidx.room:room-runtime:2.3.0-alpha02"
implementation "androidx.room:room-ktx:2.3.0-alpha02"
kapt "androidx.room:room-compiler:2.3.0-alpha02"

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...