Paging3: “Not sure how to convert a Cursor to this method's return type” when using PagingSource as return type in Room DAO

对着背影说爱祢 提交于 2020-12-05 07:28:29

问题


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()

Here is my 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>

}

My GithubUser.kt looks like this:

@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?

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.

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'

回答1:


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"


来源:https://stackoverflow.com/questions/63373069/paging3-not-sure-how-to-convert-a-cursor-to-this-methods-return-type-when-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!