diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt b/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt index 1ba009fd2..5858f7e5d 100644 --- a/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt @@ -164,18 +164,11 @@ internal class MappingSupportSQLiteDatabase( private val delegateQuery: SupportSQLiteQuery ) : SupportSQLiteQuery by delegateQuery { - private val lock = Any() - private var called = false - private val _sql = map(delegateQuery.sql) + private val sqlDelegate = OneOffDelegate { Timber.tag("MappingSupportSQLiteQuery").e("SQL queried twice") } + override val sql: String - get() = synchronized(lock) { - if (called) { - Timber.tag("MappingSupportSQLiteQuery").e("SQL queried twice") - } - called = true - return _sql - } + get() = sqlDelegate.call { _sql } } } diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt b/data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt new file mode 100644 index 000000000..8d88ad43d --- /dev/null +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/OneOffDelegate.kt @@ -0,0 +1,15 @@ +package org.cryptomator.data.db.sqlmapping + +internal class OneOffDelegate(private val beforeExtraCalls: () -> Unit) { + + private val lock = Any() + private var called = false + + fun call(delegateCallable: () -> R): R = synchronized(lock) { + if (called) { + beforeExtraCalls() + } + called = true + return delegateCallable() + } +} \ No newline at end of file