Paging3:“不确定如何将Cursor转换为该方法的返回类型"在Room DAO中使用PagingSource作为返回类型时 [英] Paging3: "Not sure how to convert a Cursor to this method's return type" when using PagingSource as return type in Room DAO

查看:188
本文介绍了Paging3:“不确定如何将Cursor转换为该方法的返回类型"在Room DAO中使用PagingSource作为返回类型时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to imitate Google's codelab for the new Paging 3 library, and I encountered the following error when I tried to have a Room DAO method return a 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
}

In the code for the Paging Codelab, the DAO method just returns a PagingSource with no extra annotations/magic options in Gradle or whatever. I also looked at other examples from Github like this and this that use the Paging 3 library, they just return PagingSource with no problems at all. Can anyone tell me if I missed something?

注意:在错误发生之前,我总是收到有关用于代码生成的ANTLR工具版本4.5.3与当前运行时版本4.7.1不匹配的警告,但此警告本身过去并没有引起任何问题,但是我在此指出是为了以防万一.

NOTE: Before the error itself, I always get a warning about ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1, but this warning itself has not caused any problems in the past, but I am noting it here just in case.

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

EDIT: I use the following Room/Paging lib versions:

    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'

推荐答案

原来,您需要将Room版本提高到 2.3.0-alpha02 或更高版本:

It turns out that you need to increase the Room version to 2.3.0-alpha02 or above:

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"

这篇关于Paging3:“不确定如何将Cursor转换为该方法的返回类型"在Room DAO中使用PagingSource作为返回类型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆