Closing the Database in a Contentprovider

Closing the database in a ContentProvider

According to Dianne Hackborn (Android framework engineer) there is no need to close the database in a content provider.

A content provider is created when its hosting process is created, and
remains around for as long as the process does, so there is no need to
close the database -- it will get closed as part of the kernel
cleaning up the process's resources when the process is killed.

Thanks @bigstones for pointing this out.

Re-create database when using Content Providers

One suggestion that comes to mind is to have your custom function drop the tables and recreate them. You already have the code to create the tables in onCreate() of DatabaseHelper. Refactor this somewhere that is accessible by both DatabaseHelper and the custom method.

ContentProvider not called onCreate after deleting database

I found solution as,

First of all I referred Refresh/Reload database reference in custom ContentProvider after restore but not satisfied with answer because its just for closing database.

So I have created my answer as below:

DBHelper.java

/**
* Delete database
*/
public static void reCreateDatabase(Context mContext) {
ContentResolver resolver = mContext.getContentResolver();
ContentProviderClient client = resolver.acquireContentProviderClient(KOOPSContentProvider.AUTHORITY);

assert client != null;
KOOPSContentProvider provider = (KOOPSContentProvider) client.getLocalContentProvider();

assert provider != null;
provider.resetDatabase();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
client.close();
else
client.release();

LOGD("Database Deleted...");
}

public void removeDatabase(Context mContext) {
mContext.deleteDatabase(DATABASE_NAME);
LOGD("Database Deleted...");
}

KOOPSContentProvider.java

public void resetDatabase() {
if(dbHelper != null) {
dbHelper.removeDatabase(getContext());
dbHelper = new DbHelper(getContext());
} else {
LOGD("Database NULL");
}
}

USE As:

DbHelper.reCreateDatabase(mContext);

Thank you :) :)

Get Cursor finalized without prior close() message even if I use ContentProvider

It's true that you don't have to close the database when using ContentProvider, but this doesn't apply for cursors. You must be using getContentResolver().query() which returns a cursor. Eventually you will have to close that returned cursor with .close(), otherwise you'll get that nasty message.

Using Content Provider Cursor

EDIT:

Make sure you only call:

getReadableDatabase()

once, as per:

How to avoid db not close and cursor exception

Whenever you've finsihed pulling information from a cursor you need to call:

cursor.close();

So if you have some call:

public void parseValsFromCursor(Cursor C) {
String val = c.getString(c.getColumnIndex(MYCOLUMN));
Log.e(TAG,val);

}

and then you try and do another db query, you'll see this error.

The fix is to modify the cursor parsing code to include the .close() method like so:

public void parseValsFromCursor(Cursor C) {
String val = c.getString(c.getColumnIndex(MYCOLUMN));
Log.e(TAG,val);
c.close();
}

Get Cursor finalized without prior close() message even if I use ContentProvider

It's true that you don't have to close the database when using ContentProvider, but this doesn't apply for cursors. You must be using getContentResolver().query() which returns a cursor. Eventually you will have to close that returned cursor with .close(), otherwise you'll get that nasty message.



Related Topics



Leave a reply



Submit