Room “Not sure how to convert a Cursor to this method's return type”: which method?

无人久伴 提交于 2019-12-03 19:17:15

问题


Error:Not sure how to convert a Cursor to this method's return type
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.

Using Room I'm getting this error and I'd like to find out which method causes it.

I have multiple DAOs, with approximately 60 methods in total, and this error just popped up after adding a method (copy&pasted from another one that worked perfectly, just changed the field to set).

I could post the whole class of DAOs, but I'm asking for a way to know which method failed. I tried with Run with --stacktrace, Run with --info and --debug option, but none of these show any valuable information.

The method I added is a @Query UPDATE with Int return type, as suggested in the documentation

UPDATE or DELETE queries can return void or int. If it is an int, the value is the number of rows affected by this query.

EDIT: I'd like to add that I tried deleting the method, bringing the DAO back to the working state, but it still gives me this error.

EDIT2: Adding gradle console output because unreadable in comments:

error: Not sure how to convert a Cursor to this method's return type
error: Not sure how to convert a Cursor to this method's return type
2 errors

:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s

回答1:


I Spend the entire day on this issue. the solution was very simple. I was using something like this before

@Query("SELECT * FROM myTable")
fun getAll(): MutableLiveData<ArrayList<myData>>

Now when I changed ArrayList to List & MutableLiveData to LiveData it is working fine.

@Query("SELECT * FROM myTable")
fun getAll(): LiveData<List<myData>>

based on the answers & comments on this issue I think room support only List & LiveData because I tried with on MutableLiveData & only ArrayList too. none of the combinations worked.

Hope this will help someones few hours.




回答2:


Yup, based on what you have mentioned in the comments, you are not allowed to change the return type from List to anything else inside the Dao. I'd assume Room doesn't know how to deal with other return types. Take the List and cast/convert it into your desired type outside of the Dao.




回答3:


Recently I've had the same problem but I was using Coroutines within the Dao function, something like this:

@Query("SELECT * FROM Dummy")
suspend fun get: LiveData<List<Dummy>>

And was unable to compile, but after removing the suspend everything worked just fine.




回答4:


In my case i was this problem when i used LiveData<ArrayList<Example Class>> in Dao class for getting all things from Room and i fixed this when i change ArrayList with List.

Example(Kotlin):

@Dao
interface ExampleDao {
@Query("SELECT * from example_table")
fun getAllExample():LiveData<List<Example>>
}



回答5:


Add the below code inside defaultConfig in build.gradle

javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true



回答6:


For my case, after got "Not sure how to convert a Cursor to this method's return type”:

delete the "build" and re-build, the error disappear.




回答7:


 class IdAndFullName {
     public int uid;
     @ColumnInfo(name = "full_name")
     public String fullName;
 }
 // DAO
 @Query("SELECT uid, name || lastName as full_name FROM user")
 public IdAndFullName[] loadFullNames();

If there is a mismatch between the query result and the POJO, Room will give you this error message.

Or if you are using @SkipQueryVerification, you will also get this error.




回答8:


Modify your Dao, use Flowable instead of observable and add the following dependency (room with rxjava support)

compile group: 'android.arch.persistence.room', name: 'rxjava2', version: '1.1.1'

Dao returns flowable:

@Query("SELECT * FROM TableX")
public abstract Flowable<List<EntityX>> getAllXs();



回答9:


For it was because of mixing AndroidX with Pre-AndroidX. After a full migration and performing this, everything was back to normal. (Of course I moved to AndroidX-Room as well)




回答10:


For me it was to change from MutableLiveData to LiveData as the return type of the get method.




回答11:


For me, I was using wrong return type for queries.




回答12:


I got this error when I was trying do some aggregate functions in the query, like sum and count and then using aliases in the column names.

select count(users.id) as userCount, ...

It so happens that the alias name like userCount above, must match the field name in the model.




回答13:


You have to include the @Relation annotation in the class returned by the method. It's the only way Room would know how to establish the relationship between the two.




回答14:


I've got a different use case for my apps.

So, I'm trying to return the actual Cursor type.

E.g:

@Query("SELECT * FROM tbl_favourite")
abstract suspend fun selectAll(): Cursor

The above code will always throw Error:Not sure how to convert a android.database.Cursor to this method's return type

But as I recall correctly, the official docs also stated here that Room supports Cursor.

After trying to debug the error log, and open up the MyTableDao_Impl.java file I've found that looks like Cursor are having an unhealthy relationship with suspend keywords.

Thus, I've corrected my code to be like this:

@Query("SELECT * FROM tbl_favourite")
abstract fun selectAll(): Cursor

And voila, it works.




回答15:


Make sure if there is

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: METRO)


error in your build log before the error you mentioned in your description. If it is, you may have forgotten to add your new entity Pojo to database class. something like this

@Database(entities = {Table.class,ForgottenTable.class}, version = 1) 
public abstract class Database extends RoomDatabase {
    //class codes
}



回答16:


Make sure you are not using suspend together with LiveData as return type:

@Query("SELECT * FROM ...")
fun getAllTellsByReceiver(receiverUid: String): LiveData<List<Tell>>



回答17:


For anyone landing here, using a coroutine Flow as a return type, you will get this error if you accidentally make the function suspend. Since it is returning a flow, there is no need to suspend.

So instead of this:

@Query("SELECT * FROM myTable WHERE id = :id")
suspend fun findById(id: Long): Flow<MyDataType>

use this (without suspend modifier):

@Query("SELECT * FROM myTable WHERE id = :id")
fun findById(id: Long): Flow<MyDataType> 


来源:https://stackoverflow.com/questions/46445964/room-not-sure-how-to-convert-a-cursor-to-this-methods-return-type-which-meth

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