Skip to content

Commit 7148451

Browse files
committed
Use upsertAll after sync instead of several individual inserts
1 parent f6548fa commit 7148451

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
@@ -304,47 +304,53 @@ class TaskRepository(
304304
val remoteTasks = withContext(Dispatchers.IO) {
305305
tasksApi.listAll(remoteListId, showHidden = true, showCompleted = true)
306306
}
307-
remoteTasks.onEach { remoteTask ->
307+
308+
val taskEntities = remoteTasks.map { remoteTask ->
308309
val existingEntity = taskDao.getByRemoteId(remoteTask.id)
309310
val parentTaskEntity = remoteTask.parent?.let { taskDao.getByRemoteId(it) }
310-
taskDao.upsert(remoteTask.asTaskEntity(localListId, existingEntity?.id, parentTaskEntity?.id))
311+
remoteTask.asTaskEntity(localListId, existingEntity?.id, parentTaskEntity?.id)
311312
}
313+
taskDao.upsertAll(taskEntities)
314+
312315
taskDao.deleteStaleTasks(localListId, remoteTasks.map(Task::id))
316+
313317
val localOnlyTasks = taskDao.getLocalOnlyTasks(localListId)
314318
val sortedRootTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == null })
315319
var previousTaskId: String? = null
320+
val syncedTasks = mutableListOf<TaskEntity>()
316321
sortedRootTasks.onEach { localRootTask ->
317-
val remoteTask = syncLocalTask(localListId, remoteListId, localRootTask, null, previousTaskId)
318-
val sortedSubTasks = computeTaskPositions(localOnlyTasks.filter { it.parentTaskLocalId == localRootTask.id })
319-
var previousSubTaskId: String? = null
320-
sortedSubTasks.onEach { localSubTask ->
321-
val remoteSubTask = syncLocalTask(localListId, remoteListId, localSubTask, remoteTask?.id, previousSubTaskId)
322-
previousSubTaskId = remoteSubTask?.id
322+
val remoteRootTask = withContext(Dispatchers.IO) {
323+
try {
324+
tasksApi.insert(remoteListId, localRootTask.asTask(), null, previousTaskId).also {
325+
syncedTasks.add(it.asTaskEntity(localListId, localRootTask.id, null))
326+
}
327+
} catch (_: Exception) {
328+
null
329+
}
323330
}
324-
previousTaskId = remoteTask?.id
325-
}
326-
}
327-
}
328331

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

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

0 commit comments

Comments
 (0)