Skip to content

Commit d38d62f

Browse files
committed
Use upsertAll after sync instead of several individual inserts
1 parent 7d0ad42 commit d38d62f

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

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
@@ -319,47 +319,53 @@ class TaskRepository(
319319
val remoteTasks = withContext(Dispatchers.IO) {
320320
tasksApi.listAll(remoteListId, showHidden = true, showCompleted = true)
321321
}
322-
remoteTasks.onEach { remoteTask ->
322+
323+
val taskEntities = remoteTasks.map { remoteTask ->
323324
val existingEntity = taskDao.getByRemoteId(remoteTask.id)
324325
val parentTaskEntity = remoteTask.parent?.let { taskDao.getByRemoteId(it) }
325-
taskDao.upsert(remoteTask.asTaskEntity(localListId, existingEntity?.id, parentTaskEntity?.id))
326+
remoteTask.asTaskEntity(localListId, existingEntity?.id, parentTaskEntity?.id)
326327
}
328+
taskDao.upsertAll(taskEntities)
329+
327330
taskDao.deleteStaleTasks(localListId, remoteTasks.map(Task::id))
331+
328332
val localOnlyTasks = taskDao.getLocalOnlyTasks(localListId)
329333
val sortedRootTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == null })
330334
var previousTaskId: String? = null
335+
val syncedTasks = mutableListOf<TaskEntity>()
331336
sortedRootTasks.onEach { localRootTask ->
332-
val remoteTask = syncLocalTask(localListId, remoteListId, localRootTask, null, previousTaskId)
333-
val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
334-
var previousSubTaskId: String? = null
335-
sortedSubTasks.onEach { localSubTask ->
336-
val remoteSubTask = syncLocalTask(localListId, remoteListId, localSubTask, remoteTask?.id, previousSubTaskId)
337-
previousSubTaskId = remoteSubTask?.id
337+
val remoteRootTask = withContext(Dispatchers.IO) {
338+
try {
339+
tasksApi.insert(remoteListId, localRootTask.asTask(), null, previousTaskId).also {
340+
syncedTasks.add(it.asTaskEntity(localListId, localRootTask.id, null))
341+
}
342+
} catch (_: Exception) {
343+
null
344+
}
338345
}
339-
previousTaskId = remoteTask?.id
340-
}
341-
}
342-
}
343346

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

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

0 commit comments

Comments
 (0)