Skip to content

Commit 45c682b

Browse files
committed
Use upsertAll after sync instead of several individual inserts
1 parent 5ba76a8 commit 45c682b

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

tasks-core/src/commonMain/kotlin/net/opatry/tasks/data/TaskRepository.kt

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -312,47 +312,53 @@ class TaskRepository(
312312
val remoteTasks = withContext(Dispatchers.IO) {
313313
tasksApi.listAll(remoteListId, showHidden = true, showCompleted = true)
314314
}
315-
remoteTasks.onEach { remoteTask ->
315+
316+
val taskEntities = remoteTasks.map { remoteTask ->
316317
val existingEntity = taskDao.getByRemoteId(remoteTask.id)
317318
val parentTaskEntity = remoteTask.parent?.let { taskDao.getByRemoteId(it) }
318-
taskDao.upsert(remoteTask.asTaskEntity(localListId, existingEntity?.id, parentTaskEntity?.id))
319+
remoteTask.asTaskEntity(localListId, existingEntity?.id, parentTaskEntity?.id)
319320
}
321+
taskDao.upsertAll(taskEntities)
322+
320323
taskDao.deleteStaleTasks(localListId, remoteTasks.map(Task::id))
324+
321325
val localOnlyTasks = taskDao.getLocalOnlyTasks(localListId)
322326
val sortedRootTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == null })
323327
var previousTaskId: String? = null
328+
val syncedTasks = mutableListOf<TaskEntity>()
324329
sortedRootTasks.onEach { localRootTask ->
325-
val remoteTask = syncLocalTask(localListId, remoteListId, localRootTask, null, previousTaskId)
326-
val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
327-
var previousSubTaskId: String? = null
328-
sortedSubTasks.onEach { localSubTask ->
329-
val remoteSubTask = syncLocalTask(localListId, remoteListId, localSubTask, remoteTask?.id, previousSubTaskId)
330-
previousSubTaskId = remoteSubTask?.id
330+
val remoteRootTask = withContext(Dispatchers.IO) {
331+
try {
332+
tasksApi.insert(remoteListId, localRootTask.asTask(), null, previousTaskId).also {
333+
syncedTasks.add(it.asTaskEntity(localListId, localRootTask.id, null))
334+
}
335+
} catch (_: Exception) {
336+
null
337+
}
331338
}
332-
previousTaskId = remoteTask?.id
333-
}
334-
}
335-
}
336339

337-
private suspend fun syncLocalTask(
338-
localTaskListId: Long,
339-
remoteTaskListId: String,
340-
localTask: TaskEntity,
341-
parentTaskId: String?,
342-
previousTaskId: String?
343-
): Task? {
344-
val remoteTask = withContext(Dispatchers.IO) {
345-
try {
346-
tasksApi.insert(remoteTaskListId, localTask.asTask(), parentTaskId, previousTaskId)
347-
} catch (_: Exception) {
348-
null
340+
// don't try syncing sub tasks if root task failed, it would break hierarchy on remote side
341+
if (remoteRootTask != null) {
342+
val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
343+
var previousSubTaskId: String? = null
344+
sortedSubTasks.onEach { localSubTask ->
345+
val remoteSubTask = withContext(Dispatchers.IO) {
346+
try {
347+
tasksApi.insert(remoteListId, localSubTask.asTask(), remoteRootTask.id, previousSubTaskId).also {
348+
val parentTaskEntity = it.parent?.let { taskDao.getByRemoteId(it) }
349+
syncedTasks.add(it.asTaskEntity(localSubTask.id, localRootTask.id, parentTaskEntity?.id))
350+
}
351+
} catch (_: Exception) {
352+
null
353+
}
354+
}
355+
previousSubTaskId = remoteSubTask?.id
356+
}
357+
}
358+
previousTaskId = remoteRootTask?.id
349359
}
360+
taskDao.upsertAll(syncedTasks)
350361
}
351-
if (remoteTask != null) {
352-
val parentTaskEntity = remoteTask.parent?.let { taskDao.getByRemoteId(it) }
353-
taskDao.upsert(remoteTask.asTaskEntity(localTaskListId, localTask.id, parentTaskEntity?.id))
354-
}
355-
return remoteTask
356362
}
357363

358364
suspend fun createTaskList(title: String): Long {

0 commit comments

Comments
 (0)