將資料刪除是 健康資料同步 CRUD 作業中重要的一環。本指南說明刪除記錄的兩種方式。
使用記錄 ID 進行刪除
您可以使用專屬 ID 清單刪除記錄,例如記錄 ID 和應用程式用戶端記錄 ID。請使用 deleteRecords,並提供兩份 Strings 清單,一份為記錄 ID 清單,另一份為用戶端 ID 清單。如果只能使用其中一種 ID,請為另一份 ID 清單設定 emptyList()。
以下程式碼範例說明如何使用 ID 刪除步數資料:
try { healthConnectClient.deleteRecords( recordType = StepsRecord::class, recordIdsList = idList, clientRecordIdsList = emptyList<String>() ) } catch (e: Exception) { // Run error handling here }
使用時間範圍進行刪除
您也可以使用時間範圍做為篩選條件來刪除資料。使用 deleteRecords,並提供可取得開始和結束時間戳記值的 TimeRangeFilter 物件。
以下程式碼範例說明如何刪除特定時間的步數資料:
try { healthConnectClient.deleteRecords( StepsRecord::class, timeRangeFilter = TimeRangeFilter.between(startTime, endTime) ) } catch (e: Exception) { // Run error handling here }