Room persistence library and Content provider

Wubbalubbadubdub picture Wubbalubbadubdub · Oct 18, 2017 · Viewed 15.6k times · Source

Last Couple of days I have been spending times on learning new Android Architecture Components . After following up some blog posts, documentation & tutorials , every components were getting clear to me . But Suddenly I realised what about our old friend Content Provider . I might sound silly , because before writing this question I have spent quite a time searching , Am I be the only one came up with this question . I hadn't got any helpful solution . Anyways here is it , if I want to build up an app with local DB , I will now obviously choose new Architecture Components (live data , view model , room ) without any farther thinking this will be very helpful to make app 10x robust . But If I want my DB datas accessible to other app , for instance To Widget How do I integrate Content Provider with Room ?

Answer

Mark picture Mark · Oct 23, 2017

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.