Use androidx transaction in databases
Replace the redundant SQLiteDatabase.execute() extension with the AndroidX SQLiteDatabase.transaction() extension, which does the same thing.
This commit is contained in:
parent
4d92df7896
commit
355b3885e3
3 changed files with 6 additions and 27 deletions
|
|
@ -4,6 +4,7 @@ import android.content.ContentValues
|
|||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import androidx.core.database.sqlite.transaction
|
||||
import org.oxycblt.auxio.logD
|
||||
|
||||
/**
|
||||
|
|
@ -33,7 +34,7 @@ class BlacklistDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n
|
|||
fun writePaths(paths: List<String>) {
|
||||
assertBackgroundThread()
|
||||
|
||||
writableDatabase.execute {
|
||||
writableDatabase.transaction {
|
||||
delete(TABLE_NAME, null, null)
|
||||
|
||||
logD("Deleted paths db")
|
||||
|
|
|
|||
|
|
@ -6,29 +6,6 @@ import android.os.Looper
|
|||
import org.oxycblt.auxio.logE
|
||||
import java.lang.Exception
|
||||
|
||||
/**
|
||||
* Shortcut for running a series of [commands] on an [SQLiteDatabase].
|
||||
* @return true if the transaction was successful, false if not.
|
||||
*/
|
||||
fun SQLiteDatabase.execute(commands: SQLiteDatabase.() -> Unit): Boolean {
|
||||
beginTransaction()
|
||||
|
||||
val success = try {
|
||||
commands()
|
||||
setTransactionSuccessful()
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
logE("An error occurred when trying to execute commands.")
|
||||
logE(e.stackTraceToString())
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
endTransaction()
|
||||
|
||||
return success
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortcut for querying all items in a database and running [block] with the cursor returned.
|
||||
* Will not run if the cursor is null.
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import android.content.Context
|
|||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import androidx.core.database.getStringOrNull
|
||||
import androidx.core.database.sqlite.transaction
|
||||
import org.oxycblt.auxio.logD
|
||||
|
||||
/**
|
||||
|
|
@ -85,7 +86,7 @@ class PlaybackStateDatabase(context: Context) :
|
|||
fun writeState(state: PlaybackState) {
|
||||
assertBackgroundThread()
|
||||
|
||||
writableDatabase.execute {
|
||||
writableDatabase.transaction {
|
||||
delete(TABLE_NAME_STATE, null, null)
|
||||
|
||||
this@PlaybackStateDatabase.logD("Wiped state db.")
|
||||
|
|
@ -159,7 +160,7 @@ class PlaybackStateDatabase(context: Context) :
|
|||
|
||||
val database = writableDatabase
|
||||
|
||||
database.execute {
|
||||
database.transaction {
|
||||
delete(TABLE_NAME_QUEUE, null, null)
|
||||
}
|
||||
|
||||
|
|
@ -171,7 +172,7 @@ class PlaybackStateDatabase(context: Context) :
|
|||
while (position < queueItems.size) {
|
||||
var i = position
|
||||
|
||||
database.execute {
|
||||
database.transaction {
|
||||
while (i < queueItems.size) {
|
||||
val item = queueItems[i]
|
||||
i++
|
||||
|
|
|
|||
Loading…
Reference in a new issue