From 3c42dce6e58a53b86512baaf1afe3061cfb8c3bd Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 6 Aug 2020 08:26:05 -0500 Subject: [PATCH] Update task query threading and transactions --- app/src/main/java/org/tasks/data/TaskDao.kt | 28 +++++++++---------- .../java/org/tasks/ui/TaskListViewModel.kt | 9 +++--- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/org/tasks/data/TaskDao.kt b/app/src/main/java/org/tasks/data/TaskDao.kt index 114a23eed..4ac07763d 100644 --- a/app/src/main/java/org/tasks/data/TaskDao.kt +++ b/app/src/main/java/org/tasks/data/TaskDao.kt @@ -95,23 +95,22 @@ abstract class TaskDao(private val database: Database) { + "WHERE completed > 0 AND calendarUri IS NOT NULL AND calendarUri != ''") abstract suspend fun clearCompletedCalendarEvents(): Int - @Transaction open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List): List { return fetchTasks(callback, getSubtaskInfo()) } - @Transaction open suspend fun fetchTasks(callback: suspend (SubtaskInfo) -> List, subtasks: SubtaskInfo): List { - val start = if (BuildConfig.DEBUG) DateUtilities.now() else 0 - val queries = callback.invoke(subtasks) - val db = database.openHelper.writableDatabase - val last = queries.size - 1 - for (i in 0 until last) { - db.execSQL(queries[i]) + return database.withTransaction { + val start = if (BuildConfig.DEBUG) DateUtilities.now() else 0 + val queries = callback.invoke(subtasks) + val last = queries.size - 1 + for (i in 0 until last) { + query(SimpleSQLiteQuery(queries[i])) + } + val result = fetchTasks(SimpleSQLiteQuery(queries[last])) + Timber.v("%sms: %s", DateUtilities.now() - start, queries.joinToString(";\n")) + result } - val result = fetchTasks(SimpleSQLiteQuery(queries[last])) - Timber.v("%sms: %s", DateUtilities.now() - start, queries.joinToString(";\n")) - return result } suspend fun fetchTasks(preferences: Preferences, filter: Filter): List { @@ -121,7 +120,10 @@ abstract class TaskDao(private val database: Database) { } @RawQuery - abstract suspend fun fetchTasks(query: SimpleSQLiteQuery): List + internal abstract suspend fun query(query: SimpleSQLiteQuery): Int + + @RawQuery + internal abstract suspend fun fetchTasks(query: SimpleSQLiteQuery): List @RawQuery 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") abstract suspend fun setLastNotified(id: Long, timestamp: Long): Int - @Transaction open suspend fun fetchChildren(id: Long): List { 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") abstract suspend fun setCollapsed(id: Long, collapsed: Boolean) - @Transaction open suspend fun setCollapsed(preferences: Preferences, filter: Filter, collapsed: Boolean) { fetchTasks(preferences, filter) .filter(TaskContainer::hasChildren) diff --git a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt index 2a9ae843b..a64c9607d 100644 --- a/app/src/main/java/org/tasks/ui/TaskListViewModel.kt +++ b/app/src/main/java/org/tasks/ui/TaskListViewModel.kt @@ -55,7 +55,7 @@ class TaskListViewModel @ViewModelInject constructor( return } try { - viewModelScope.launch { + viewModelScope.launch(Dispatchers.Default) { val subtasks = taskDao.getSubtaskInfo() if (manualSortFilter || !preferences.usePagedQueries()) { performNonPagedQuery(subtasks) @@ -69,9 +69,10 @@ class TaskListViewModel @ViewModelInject constructor( } private suspend fun performNonPagedQuery(subtasks: SubtaskInfo) { - tasks.value = taskDao.fetchTasks( - { s: SubtaskInfo? -> getQuery(preferences, filter!!, s!!) }, - subtasks) + tasks.postValue( + taskDao.fetchTasks( + { s: SubtaskInfo? -> getQuery(preferences, filter!!, s!!) }, + subtasks)) } private fun performPagedListQuery() {