3
votes

I have multiple cursor loaders set up pointing to different URIs in my content provider and they work without any issues. Each loader is for a specific table in my database.

However I have now created a new URI in my content provider to execute a join query on my tables. I have created a new loader that points to the URI to be used for performing a join query but the loader doesn't update the UI.

If I close the app and then open it then the new data appears but after inserting new data in one activity and then returning to my main activity, the data hasn't updated. The loader isn't being called despite the database have new data inserted to it.

I am calling cursor.setNotificationUri(getContext().getContentResolver(), uri); for my new join URI.

Is there any particular reason why this isn't working? Like I said the loaders work fine for standard querying of individual tables but a loader with a join query doesn't.

Is it that a loader can only listen to a single table being updated hence why it can't listen to the join?

EDIT:

Some more information about the workflow...

Activity 1 reads data using a join query of the separate tables and displays to the UI Activity 2 inserts data into separate tables to the database

Activity 1 launches Activity 2 which finishes after inserting data

Activity 1 has a loaders to update the UI. Loaders reading from individual tables works fine but a loader reading from a join query doesn't get called when data is updated. It is only called when the activity is created.

1
Are you inserting the new data using the join or the separate Uris? I think if you insert to A or B then only Cursors for A or B are refreshed, but if you have a Cursor for A join B then inserting to A is not going to refresh those Cursors. How would it know which other Uris depend on A for example? Mind you I haven't yet gotten into ContentProviders! I hope you find an answer, I'm curious too. In the meantime you can try in onResume: getLoaderManager().getLoader(id).onContentChanged(); - TWiStErRob
Data is inserted into tables separately and then read back in a join. What you have stated could be the cause of the problem. - user2844485
@TWiStErRob Do you suggest using onResume: getLoaderManager().getLoader(id).onContentChanged(); is a good practice? It works but I wonder if it is the best option. - user2844485
Until we can't answer "How would it know which other Uris depend on A?" that's the best bet to always show latest data. I'm using DB cursors not a ContentProvider cursor, so I must call it. I'm guarding it by !getLoaderManager().hasRunningLoaders(). - TWiStErRob
I would try using a view (keep dp in the database. - danny117

1 Answers

1
votes

I Know this question was asked a long time ago but hopefully it will help some others experiencing the same problem... I had exactly the same issue

In your Query method in your Content Provider Class, before you set the notification Uri. Make sure the Update and Delete Uri's are the same as the query Uri's.

uri = Uri.parse("content://com.casselsinthecloud.cloudbeats.provider/playlist_contents");

Then call setNotificationUri(...) with the new Uri..

cursor.setNotificationUri(getContext().getContentResolver(), uri);

This will ensure the CursorLoader is updated dynamically..