Earlier on, I had a lot of problem with Database not open and database not closed exceptions. I sort of brute-forced a solution to that by just opening and close the database every time I accessed it. It was ok for a while, but when I added SRS, it slowed things down. I adjusted the database code to only open once, and never close, in an attempt to speed things up. It didn't work - most of the time is being take in the update and delete. However, since I'm not closing the sqllite file, the DatabaseObjectNotClosed exception is rearing it's ugly head again.
I'm not sure when or why it decides to call it. But, what I think I'll do is close it and set it to null on the on pause for each activity. That will cause it to be reopened, since the "createDB" procedure, which is called everywhere, always check for myDatabase = null.
First, let's see if we can repeat the error. I'll start up the app, then go somewhere else.
Ok, I got the message right away when I re-opened the program. Let's exit it.
I'm thinking this is the message that gets created when I leave the program:
D/dalvikvm( 7314): GC_EXPLICIT freed 167K, 53% free 2945K/6215K, external 3584K/4005K, paused 74ms
That looks familiar.
Ok, when I came back in after explicity killing the program with a taskkiller, I got the message again. Let's try one more time:
Yup. Ok, first I'm just going to put the close / null in the QuestionActivity. The reason I think I need to set it to null is I don't trust the "isOpen" function of the SQLlite database, but I know for sure the check for null works.
Nope, didn't work. So, what if I close it every time, like I was before?
Ok, well, I could go back to the old way of opening and closing every time. But, that seems inefficient.
Well, how about google? Look at this from StackOverflow:
startManagingCursor(Cursor)
"You can also use startManagingCursor(Cursor) to let Android manage the cursor for you. This is the most useful if you are binding the data to a ListView."
Let's look at that. Oh, it's deprecated. They recommend using CursorLoader with ContentResolver.
I like the idea of convert to ContentResolver. It seems safer. I saw at least one SO post where he said it resolved his corrupt DB problems. It's just that I've got this database code working right now. On the other hand, what's another day?
Ah, here was the problem:
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
Log.d(TAG, "checkDatZabase entry");
SQLiteDatabase checkDB = null;
try {
String myPath = DataBaseHelper.getRootPathName() + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
// checkDB.close();
}
Log.d(TAG, "checkDatabase exit");
return checkDB != null ? true : false;
}
For some reason, I had commented out the checkDb.close().
Ok, that got rid of a problem coming and going to the question activity, but it's still left open when the process is killed.
Ok, well, I'm going wait on this one. Instead of mucking around with this legacy code, I'll implement the ContentResolver. That's supposed to take care of all these problems.
No comments:
Post a Comment