Paging3:"Not sure how to convert a Cursor to this method's return type" 将 PagingSource 用作 Room DAO 中的 return 类型
Paging3: "Not sure how to convert a Cursor to this method's return type" when using PagingSource as return type in Room DAO
我试图为新的 Paging 3 库模仿 Google 的代码实验室,当我尝试使用 Room DAO 方法 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
}
在使用 Paging 3 库的 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 中,它们 return PagingSource
完全没有问题。谁能告诉我是否遗漏了什么?
注意:在错误本身之前,我总是收到关于 ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1
的警告,但这个警告本身在过去并没有引起任何问题,但我以防万一。
编辑:我使用以下 Room/Paging 库版本:
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
以上:
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"
我试图为新的 Paging 3 库模仿 Google 的代码实验室,当我尝试使用 Room DAO 方法 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
}
在使用 Paging 3 库的 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 中,它们 return PagingSource
完全没有问题。谁能告诉我是否遗漏了什么?
注意:在错误本身之前,我总是收到关于 ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1
的警告,但这个警告本身在过去并没有引起任何问题,但我以防万一。
编辑:我使用以下 Room/Paging 库版本:
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
以上:
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"