I am using a SQLite backed ContentProvider to cache server data locally. I want to attach a timestamp to the entire ContentProvider so that I can very quickly check if data is up-to-date by comparing the timestamp to that of the server. One easy solution is to have an update column. But that is a bad idea since I would be copying the data for each row; whereas I only want the data once. Is there a way to save this data to disk without having to duplicate it every time? I imaging a SharedPreference-Cursor combo can do it. But, instead, is there a way to save such data in the SQLite DB itself? Or at least within the ContentProvider and be able to query it when I need it? From what I understand the contentProvider has a set interface that must match ContentResolver. So how do I save a timestamp inside the content provider and be able to read it from my app: without having to do it in every row?
Creatively I can create a table with one row for the timestamp only. But is that the best answer? Or is there a more standard way of doing this?
I know it seems like I have the answer to my own question (i.e. timestamp table of one row), but I don’t want to hack it if there is a standard or better way of doing this sort of thing.
I imaging a SharedPreference-Cursor combo can do it.
If it's only used internally by your provider, SharedPreferences will work just fine. Even if you do need it in a Cursor, if you have a URI just to query just for the timestamp, you could use MatrixCursor
to build a Cursor programmatically with just that one value in it.
Creatively I can create a table with one row for the timestamp only. But is that the best answer? Or is there a more standard way of doing this?
This will work as well. If you actually need this timestamp to appear in all rows returned by some queries, this might be a better option since you can simply join with this table to produce a cursor that has the timestamp value on all rows.
From what I understand the contentProvider has a set interface that must match ContentResolver.
That only determines what method signatures ContentProvider has. There is no rule that your query
method actually query a SQLiteDatabase. Again I refer you to MatrixCursor
.
One last thing that might interest you is ContentResolver.call()
. You can use this to implement a custom method and pass data back to the caller in a Bundle
. Just override call()
in your ContentProvider and handle the arguments appropriately.