Skip to content

Fix pet record Blood/Allergies display and CloudKit tombstone conflict on delete/set-local - #52

Merged
furfarch merged 2 commits into
mainfrom
copilot/fix-record-display-sync-issues
Feb 21, 2026
Merged

Fix pet record Blood/Allergies display and CloudKit tombstone conflict on delete/set-local#52
furfarch merged 2 commits into
mainfrom
copilot/fix-record-display-sync-issues

Conversation

Copilot AI commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Blood and Allergies sections were incorrectly shown in the viewer for pet records despite being suppressed in the editor. Both "Set Local" and cloud record deletion silently failed to write CloudKit tombstones due to a save conflict, preventing deletions from propagating to other devices.

Pet record viewer: hide Blood & Allergies

The editor already guards these sections with if record.isPet { EmptyView() }. The viewer (both card TabView and list Form) had no equivalent guard. Added if !record.isPet around Blood and Allergies in both cardPages and viewerFormList.

CloudKit tombstone: fix "record to insert already exists"

createTombstone was constructing a fresh CKRecord(recordType:, recordID:) without a recordChangeTag. CloudKit treats any save without a change tag as an INSERT — rejected when the record already exists in CloudKit.

This silently broke both callers:

  • deleteSyncRecord → error thrown and shown to the user
  • disableCloud (Set Local) → error caught and logged only; tombstone never written; other devices never saw the deletion

Fix: fetch the existing CKRecord first to obtain its change tag, then apply tombstone fields and save as an UPDATE. Fall back to INSERT only when the record is genuinely absent.

let ckRecord: CKRecord
do {
    ckRecord = try await database.record(for: ckID)  // gets recordChangeTag
} catch {
    if let ck = error as? CKError, ck.code == .unknownItem {
        ckRecord = CKRecord(recordType: medicalRecordType, recordID: ckID)
    } else { throw error }
}
// apply tombstone fields, then save as UPDATE

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Hide Blood and Allergies viewer sections for pet records (card viewer + list viewer)
  The editor already hides them via `if record.isPet { EmptyView() }`; viewer now matches.
- Fix createTombstone to fetch existing CKRecord before saving, preventing
  'record to insert already exists' error when deleting/setting-local a synced record.
  This also fixes 'Set Local' not propagating deletion to other devices.

Co-authored-by: furfarch <121972251+furfarch@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix display and syncing issues for pet records Fix pet record Blood/Allergies display and CloudKit tombstone conflict on delete/set-local Feb 20, 2026
Copilot AI requested a review from furfarch February 20, 2026 20:16
@furfarch
furfarch marked this pull request as ready for review February 21, 2026 11:28
Copilot AI review requested due to automatic review settings February 21, 2026 11:28
@furfarch
furfarch merged commit 3a57437 into main Feb 21, 2026
3 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes two distinct issues in the Purus Health app: incorrect display of Blood and Allergies sections for pet records in the viewer, and a CloudKit tombstone creation failure that prevented deletion sync across devices.

Changes:

  • Hide Blood and Allergies sections in the record viewer for pet records (both card and list views) to match the editor behavior
  • Fix CloudKit tombstone creation by fetching existing records to obtain recordChangeTag before updating, resolving "record to insert already exists" errors

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
PurusHealth/Views/RecordEditorView.swift Added if !record.isPet guards around Blood and Allergies sections in both cardPages and viewerFormList to hide these human-only sections for pet records
PurusHealth/Services/CloudSyncService.swift Modified createTombstone to fetch existing CloudKit record first to obtain recordChangeTag, enabling UPDATE operations instead of failing INSERTs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants