Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 412
akubi Avatar asked Aug 12 '20 08:08

akubi


Video Answer


1 Answers

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"
like image 200
Reza Bigdeli Avatar answered Sep 21 '22 15:09

Reza Bigdeli