I am trying to chnage my code to make use of room database APIs. For documents table I have defined Entity class Document
, when I query getAll()
it returns me all the document.
Now I have old implementation of Adapter which makes user of Cursor
( Its a CursorAdapter
). In my DocumentDao
class I defined one method to get list of cursor objects. My Dao class as follows :
@Dao
public interface DocumentDao {
@Query("SELECT * FROM documents")
List<com.myapp.room.entity.Document> getAll();
@Query("SELECT * FROM documents")
List<Cursor> getCursorAll();
}
During compile time I am getting following error :
Error:(20, 18) error: Not sure how to convert a Cursor to this method's return type
The official guide for Room which states that :
If your app's logic requires direct access to the return rows, you can return a Cursor object from your queries, as shown in the following code snippet:
@Dao
public interface MyDao {
@Query("SELECT * FROM user WHERE age > :minAge LIMIT 5")
public Cursor loadRawUsersOlderThan(int minAge);
}
My question is do I need to write converter for this purpose ?
When you use the Room persistence library to store your app's data, you interact with the stored data by defining data access objects, or DAOs. Each DAO includes methods that offer abstract access to your app's database. At compile time, Room automatically generates implementations of the DAOs that you define.
Room is a persistence library that provides an abstraction layer over the SQLite database to allow a more robust database. With the help of room, we can easily create the database and perform CRUD operations very easily.
android.arch.persistence.room.Dao. Marks the class as a Data Access Object. Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class.
You're returning a List<Cursor>
instead of a Cursor
. Change:
@Query("SELECT * FROM documents")
List<Cursor> getCursorAll();
for
@Query("SELECT * FROM documents")
Cursor getCursorAll();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With