forked from agzam/vyatsu-schedule
perf(teacher): lower sync memory footprint
- Reduce sync parallelism 5 -> 2 to cap peak heap while bulk-fetching all departments for a period - Replace per-write rows (insert schedule/days/lessons separately) with a single @Transaction replaceSchedule() so Room invalidates observers once per department instead of ~4x each, cutting main-thread recompositions and intermediate allocations during the 38-department sync
This commit is contained in:
@@ -4,6 +4,7 @@ import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
@@ -38,6 +39,16 @@ interface TeacherScheduleDao {
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertLessons(lessons: List<TeacherLessonEntity>)
|
||||
|
||||
@Transaction
|
||||
suspend fun replaceSchedule(schedule: TeacherScheduleEntity, days: List<TeacherDayEntity>, lessons: List<TeacherLessonEntity>) {
|
||||
val scheduleId = insertSchedule(schedule)
|
||||
val daysWithScheduleId = days.map { it.copy(scheduleId = scheduleId) }
|
||||
deleteDaysForSchedule(scheduleId)
|
||||
val dayIds = insertDays(daysWithScheduleId)
|
||||
val dayIdByDate = daysWithScheduleId.zip(dayIds).associate { (day, id) -> day.date to id }
|
||||
insertLessons(lessons.map { lesson -> lesson.copy(dayId = dayIdByDate.getValue(lesson.date)) })
|
||||
}
|
||||
|
||||
@Query(
|
||||
"SELECT teacher_days.* FROM teacher_days INNER JOIN teacher_schedules ON teacher_schedules.id = teacher_days.scheduleId " +
|
||||
"WHERE teacher_schedules.season = :season AND teacher_schedules.weekStart <= :weekStart " +
|
||||
|
||||
Reference in New Issue
Block a user