Room persistence library and Content provider

I had the same question by the way. And I found a sample here which answers my question. Hope it does the same with you.

In short, this is in the DAO object which would be called from Content Provider's query() method.

/**
 * Select all cheeses.
 *
 * @return A {@link Cursor} of all the cheeses in the table.
 */
@Query("SELECT * FROM " + Cheese.TABLE_NAME)
Cursor selectAll();

Notice how it returns Cursor object. Other operations, you can see for yourself in more detail in the sample.

This here is choice number 3 in the answer by @CommonsWare, I think.


if I want to build up an app with local DB , I will now obviously choose new Architecture Components (live data , view model , room )

I would not use the term "obviously" there. The Architecture Components are an option, but not a requirement.

But If I want my DB datas accessible to other app , for instance To Widget How do I integrate Content Provider with Room ?

An app widget is unrelated to a ContentProvider. IMHO very few apps should be exposing databases to third parties via ContentProvider, and no apps should be using a ContentProvider purely for internal purposes.

That being said, you have a few choices:

  1. Do not use Room, at least for the tables to be exposed via the ContentProvider

  2. Use Room for internal purposes, but then use classic SQLite programming techniques for the ContentProvider, by calling getOpenHelper() on your RoomDatabase

  3. Use Room in the ContentProvider, writing your own code to build up a MatrixCursor from the Room entities that you retrieve (for query()) or creating the entities for use with other operations (for insert(), update(), delete(), etc.)