Update task query threading and transactions

pull/1066/head
Alex Baker 4 years ago
parent 89f6b2c477
commit 3c42dce6e5

@ -95,23 +95,22 @@ abstract class TaskDao(private val database: Database) {
+ "WHERE completed > 0 AND calendarUri IS NOT NULL AND calendarUri != ''") + "WHERE completed > 0 AND calendarUri IS NOT NULL AND calendarUri != ''")
abstract suspend fun clearCompletedCalendarEvents(): Int abstract suspend fun clearCompletedCalendarEvents(): Int
@Transaction
open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List<String>): List<TaskContainer> { open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List<String>): List<TaskContainer> {
return fetchTasks(callback, getSubtaskInfo()) return fetchTasks(callback, getSubtaskInfo())
} }
@Transaction
open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List<String>, subtasks: SubtaskInfo): List<TaskContainer> { open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List<String>, subtasks: SubtaskInfo): List<TaskContainer> {
return database.withTransaction {
val start = if (BuildConfig.DEBUG) DateUtilities.now() else 0 val start = if (BuildConfig.DEBUG) DateUtilities.now() else 0
val queries = callback.invoke(subtasks) val queries = callback.invoke(subtasks)
val db = database.openHelper.writableDatabase
val last = queries.size - 1 val last = queries.size - 1
for (i in 0 until last) { for (i in 0 until last) {
db.execSQL(queries[i]) query(SimpleSQLiteQuery(queries[i]))
} }
val result = fetchTasks(SimpleSQLiteQuery(queries[last])) val result = fetchTasks(SimpleSQLiteQuery(queries[last]))
Timber.v("%sms: %s", DateUtilities.now() - start, queries.joinToString(";\n")) Timber.v("%sms: %s", DateUtilities.now() - start, queries.joinToString(";\n"))
return result result
}
} }
suspend fun fetchTasks(preferences: Preferences, filter: Filter): List<TaskContainer> { suspend fun fetchTasks(preferences: Preferences, filter: Filter): List<TaskContainer> {
@ -121,7 +120,10 @@ abstract class TaskDao(private val database: Database) {
} }
@RawQuery @RawQuery
abstract suspend fun fetchTasks(query: SimpleSQLiteQuery): List<TaskContainer> internal abstract suspend fun query(query: SimpleSQLiteQuery): Int
@RawQuery
internal abstract suspend fun fetchTasks(query: SimpleSQLiteQuery): List<TaskContainer>
@RawQuery @RawQuery
abstract suspend fun count(query: SimpleSQLiteQuery): Int abstract suspend fun count(query: SimpleSQLiteQuery): Int
@ -152,7 +154,6 @@ SELECT EXISTS(SELECT 1 FROM tasks WHERE parent > 0 AND deleted = 0) AS hasSubtas
@Query("UPDATE tasks SET lastNotified = :timestamp WHERE _id = :id AND lastNotified != :timestamp") @Query("UPDATE tasks SET lastNotified = :timestamp WHERE _id = :id AND lastNotified != :timestamp")
abstract suspend fun setLastNotified(id: Long, timestamp: Long): Int abstract suspend fun setLastNotified(id: Long, timestamp: Long): Int
@Transaction
open suspend fun fetchChildren(id: Long): List<Task> { open suspend fun fetchChildren(id: Long): List<Task> {
return fetch(getChildren(id)) return fetch(getChildren(id))
} }
@ -178,7 +179,6 @@ SELECT EXISTS(SELECT 1 FROM tasks WHERE parent > 0 AND deleted = 0) AS hasSubtas
@Query("UPDATE tasks SET collapsed = :collapsed WHERE _id = :id") @Query("UPDATE tasks SET collapsed = :collapsed WHERE _id = :id")
abstract suspend fun setCollapsed(id: Long, collapsed: Boolean) abstract suspend fun setCollapsed(id: Long, collapsed: Boolean)
@Transaction
open suspend fun setCollapsed(preferences: Preferences, filter: Filter, collapsed: Boolean) { open suspend fun setCollapsed(preferences: Preferences, filter: Filter, collapsed: Boolean) {
fetchTasks(preferences, filter) fetchTasks(preferences, filter)
.filter(TaskContainer::hasChildren) .filter(TaskContainer::hasChildren)

@ -55,7 +55,7 @@ class TaskListViewModel @ViewModelInject constructor(
return return
} }
try { try {
viewModelScope.launch { viewModelScope.launch(Dispatchers.Default) {
val subtasks = taskDao.getSubtaskInfo() val subtasks = taskDao.getSubtaskInfo()
if (manualSortFilter || !preferences.usePagedQueries()) { if (manualSortFilter || !preferences.usePagedQueries()) {
performNonPagedQuery(subtasks) performNonPagedQuery(subtasks)
@ -69,9 +69,10 @@ class TaskListViewModel @ViewModelInject constructor(
} }
private suspend fun performNonPagedQuery(subtasks: SubtaskInfo) { private suspend fun performNonPagedQuery(subtasks: SubtaskInfo) {
tasks.value = taskDao.fetchTasks( tasks.postValue(
taskDao.fetchTasks(
{ s: SubtaskInfo? -> getQuery(preferences, filter!!, s!!) }, { s: SubtaskInfo? -> getQuery(preferences, filter!!, s!!) },
subtasks) subtasks))
} }
private fun performPagedListQuery() { private fun performPagedListQuery() {

Loading…
Cancel
Save