Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting the database when an Account is removed

My app uses the SyncAdapter pattern, holding user credentials using the AccountManager and a ContentProvider to store data in a db.

When the account gets removed I can remove the db using the approach explained in this question. The db gets removed by doing:

boolean deleted = mContext.deleteDatabase(DatabaseHelper.DATABASE_NAME);

This works fine but when I do the login again everything is still there. It feels like the ContentProvider doesn't know that the db has been removed.

In this answer, inazaruk says:

You need to make sure you've killed the process that hosts ContentProvider that uses that specific database file. And only than delete it.

Killing the process to clear a db doesn't feel right.

Is there a better thing to do?

like image 393
Macarse Avatar asked Nov 04 '22 18:11

Macarse


1 Answers

If I'd had to do that I would try it the following way:

add some Uri that when you insert or delete using that Uri triggers database deletion inside your ContentProvider. When deleting also clear all references to the SQLiteDatabase since it is possible that you can still access the old database file through that (If you delete a file in Linux and you have that file open you can still use it - it's just no longer accessible via the path).

By putting the deletion inside the ContentProvider you should be able to close the database connection and track the deletion state in a way that you know that you need to recreate the database file.

ContentProviders don't quit unless you kill your app so you probably have the same instance running and probably references to the old file as mentioned above

like image 138
zapl Avatar answered Nov 15 '22 00:11

zapl