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,10 +188,15 @@ 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
if (cursor != null) { try {
val currEvents = fillEvents(cursor) cursor = getEventsCursor(selection, null)
events.addAll(currEvents) if (cursor != null) {
val currEvents = fillEvents(cursor)
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,22 +221,22 @@ 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()) {
do {
if (cursor.moveToFirst()) { val id = cursor.getIntValue(COL_ID)
do { val startTS = cursor.getIntValue(COL_START_TS)
val id = cursor.getIntValue(COL_ID) val endTS = cursor.getIntValue(COL_END_TS)
val startTS = cursor.getIntValue(COL_START_TS) val reminderMinutes = cursor.getIntValue(COL_REMINDER_MINUTES)
val endTS = cursor.getIntValue(COL_END_TS) val repeatInterval = cursor.getIntValue(COL_REPEAT_INTERVAL)
val reminderMinutes = cursor.getIntValue(COL_REMINDER_MINUTES) val title = cursor.getStringValue(COL_TITLE)
val repeatInterval = cursor.getIntValue(COL_REPEAT_INTERVAL) val description = cursor.getStringValue(COL_DESCRIPTION)
val title = cursor.getStringValue(COL_TITLE) events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval))
val description = cursor.getStringValue(COL_DESCRIPTION) } while (cursor.moveToNext())
events.add(Event(id, startTS, endTS, title, description, reminderMinutes, repeatInterval)) }
} while (cursor.moveToNext()) } finally {
cursor?.close()
} }
cursor.close()
return events return events
} }