Modern Apps git

← Modern-Apps.git

health: fix duplicate local record after Health Connect write

Commit
d9a11c847af4d0837599ddb2ac31d87a69e0c377
Author
Vayun Mathur <[email protected]>
When
2026-11-07 19:55 UTC (28 days ago)
Parents
7640534
Symptom: every logged hydration/body/nutrition record that mirrored
successfully to Health Connect appeared twice in Room (the pre-mirror
row plus the re-keyed row), inflating sums and history.

Root cause: writeHealthRecord called deleteByIds(listOf(record.primaryKey)).
HealthDao.deleteByIds deletes WHERE id IN (:ids), but primaryKey is
"$id-$index" (e.g. "uuid-0"), which never matches the id column
("uuid"), so the delete was a no-op. deleteRecord and HealthSyncWorker
correctly pass record.id / HC record ids.

Fix: pass record.id, matching the DAO contract.

Verification: code inspection of Record.kt:141-142 vs HealthAPI
call sites; :health:compileDevKotlin passes (run with full suite
after all fixes).

Diff

health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt
diff --git a/health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt b/health/src/main/java/com/vayunmathur/health/util/HealthAPI.ktindex 9f9aa7a773..14b5df5d8c 100644--- a/health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt+++ b/health/src/main/java/com/vayunmathur/health/util/HealthAPI.kt@@ -132,8 +132,10 @@ object HealthAPI {             val newId = response.recordIdsList.firstOrNull()             if (newId != null) {                 Log.status("HealthAPI", "Successfully wrote record to Health Connect with ID: $newId")-                // Remove old local record and replace with one containing HC ID-                repo().deleteByIds(listOf(record.primaryKey))+                // Remove old local record and replace with one containing HC ID.+                // deleteByIds matches the `id` column, not primaryKey ("$id-$index"),+                // so passing primaryKey deleted nothing and left a duplicate.+                repo().deleteByIds(listOf(record.id))                 val updatedRecord = record.copy(id = newId, primaryKey = "$newId-${record.index}")                 repo().upsert(listOf(updatedRecord))             }