Search code examples
androidsqlitecursorandroid-contentprovider

Notify cursor of one table on changes in another table


Suppose I have CursorLoader listening for changes in table1 addressed with CONTENT_URI_1. The data in table1 depends on contents of table2 addressed with CONTENT_URI_2 (For those who interested in details, table1 is sql view over table2).

Any loaders are notified of changes of tables contents via setNotificationUri() in my content provider.

Now, from some other spot I'm inserting a row into table2, which sends notifications of CONTENT_URI_2 changes. But CONTENT_URI_1 is not being notified, because there could be only one notification uri per cursor.

The question is how can I notify CONTENT_URI_1 when CONTENT_URI_2 changes?


Solution

  • I found the answer shortly after posting the question :)

    If you want to notify CONTENT_URI_1 (table1) after, for instance, inserting data into CONTENT_URI_2 (table2), you should do the following:

    Uri resultUri = context.getContentResolver().insert(CONTENT_URI_2, table2rowDataContentValues);
    // simply notify whatever URI you desire of changes
    context.getContentResolver().notifyChange(CONTENT_URI_1, null);
    

    Pretty straightforward but didn't come to me at first.