19

我试图为新的 Paging 3 库模仿 Google 的 codelab,当我尝试让 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
}

在 Paging Codelab 的代码中,DAO 方法只返回一个PagingSource在 Gradle 或其他任何东西中没有额外注释/魔术选项的方法。我还查看了来自 Github 的其他示例,例如使用 Paging 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'
4

1 回答 1

37

原来你需要将Room版本提高到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"
于 2020-08-13T10:10:10.927 回答