make sure we close the cursor after db operations

This commit is contained in:
tibbi 2016-11-19 17:18:42 +01:00
parent ba69073e02
commit 98e1508a88
1 changed files with 25 additions and 20 deletions

View File

@ -188,11 +188,16 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
private fun getEvents(selection: String): List<Event> { private fun getEvents(selection: String): List<Event> {
val events = ArrayList<Event>() val events = ArrayList<Event>()
val cursor = getEventsCursor(selection, null) var cursor: Cursor? = null
try {
cursor = getEventsCursor(selection, null)
if (cursor != null) { if (cursor != null) {
val currEvents = fillEvents(cursor) val currEvents = fillEvents(cursor)
events.addAll(currEvents) events.addAll(currEvents)
} }
} finally {
cursor?.close()
}
return events return events
} }
@ -208,7 +213,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
val builder = SQLiteQueryBuilder() val builder = SQLiteQueryBuilder()
builder.tables = "$MAIN_TABLE_NAME LEFT OUTER JOIN $META_TABLE_NAME ON $COL_EVENT_ID = $MAIN_TABLE_NAME.$COL_ID" builder.tables = "$MAIN_TABLE_NAME LEFT OUTER JOIN $META_TABLE_NAME ON $COL_EVENT_ID = $MAIN_TABLE_NAME.$COL_ID"
val projection = allColumns val projection = allColumns
return builder.query(mDb, projection, selection, selectionArgs, "$MAIN_TABLE_NAME.COL_ID", null, COL_START_TS) return builder.query(mDb, projection, selection, selectionArgs, "$MAIN_TABLE_NAME.$COL_ID", null, COL_START_TS)
} }
private val allColumns: Array<String> private val allColumns: Array<String>
@ -216,10 +221,8 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
private fun fillEvents(cursor: Cursor?): List<Event> { private fun fillEvents(cursor: Cursor?): List<Event> {
val events = ArrayList<Event>() val events = ArrayList<Event>()
if (cursor == null) try {
return events if (cursor != null && cursor.moveToFirst()) {
if (cursor.moveToFirst()) {
do { do {
val id = cursor.getIntValue(COL_ID) val id = cursor.getIntValue(COL_ID)
val startTS = cursor.getIntValue(COL_START_TS) val startTS = cursor.getIntValue(COL_START_TS)
@ -231,7 +234,9 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DB_NAME, null, DB_V
events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval)) events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval))
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
cursor.close() } finally {
cursor?.close()
}
return events return events
} }