mardi 26 mai 2015

What options are there for deleting a file when a SQLite row holding a reference to the file is deleted?

I currently have a ContentProvider that manages access to a database, which has several tables linked through foreign keys. One table maintains a list of (app-specific) audio files. There is also a table of sets; rows in the audio table must have an associated row in the set table (many-to-one).

ON CASCADE DELETE is active on the tables, so when rows in the set table are deleted, the rows in the audio table that referenced the set are also deleted.

My ContentProvider also deletes the referenced file when a call to delete() is made that targets the audio table.

The database side, therefore, works well. However, if the rows in the audio table are deleted, the files remain. I'd like to find a way of automatically removing those files.

One possible options seems to be to use a SQL trigger to run the undocumented _DELETE_FILE() function, a function apparently available in Android SQLite. Apparently the Android MediaProvider class uses this approach to delete files automatically.

  1. Is using the _DELETE_FILE() function likely to work at all?

  2. Would using _DELETE_FILE() be a violation of good practice, or indeed safe practice?

  3. Is there a better, or more advisable way to achieve this automatic deletion of unreferenced files, perhaps through the ContentProvider? Approaches that require periodic scanning for unreferenced files strike me as inelegant, and ones that require checking

Similar question: How can I automatically delete a file referenced by a SQLite Database item when this item gets deleted from the database (in Android)?

Aucun commentaire:

Enregistrer un commentaire