Fix CloudKit data loss on delete-then-reinsert#421
Open
jsutula wants to merge 2 commits intopointfreeco:mainfrom
Open
Fix CloudKit data loss on delete-then-reinsert#421jsutula wants to merge 2 commits intopointfreeco:mainfrom
jsutula wants to merge 2 commits intopointfreeco:mainfrom
Conversation
332871e to
1f5150a
Compare
1f5150a to
bdd291d
Compare
jsutula
commented
Mar 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #418
Root cause
Reinsertion of a record after a delete does not produce a new pending
.saveRecordchange entry due to missing "undelete" state detection in afterInsert trigger. No new.saveRecordmeans that only the.deleteRecordis propagated.Solution
Two parts:
1. Prerequisite change necessary for writing accurate tests: Update
MockSyncEngineStateto deduplicate changes across types like the realCKSyncEngine.StateFrom
CKSyncEngine.State.add(pendingRecordZoneChanges:)documentation:MockSyncEngineState.add(pendingRecordZoneChanges:)deduplicates fully identical changes (type+ID) by virtue of the underlyingOrderedSet, but does not deduplicate when the type (save vs delete) is different, as is described above withCKSyncEngine.State.MockSyncEngineState.add(pendingDatabaseChanges:)has a similar issue. The fix: update both to deduplicate based on ID alone which will allow for the cross-type deduplication described above, and add tests.2. Primary fix: Detect "undelete" on reinsertion in order to queue new
.saveRecordchangeUpdate the
afterInserttrigger to conditionally reset_isDeletedto false if it's been set to true (as it will be in a delete-then-reinsert scenario), and updateuserModificationTimeto the current time (as would occur in a normal insert or update). Hook up a newafterUndeleteTriggerthat listens for this specific change and queues a.saveRecordchange viasyncEngine.$didUpdate(just as would occur on a normal insert or update).Testing
In addition to new unit tests, performed a manual test with 9ea5bdd applied to verify that the data loss issue no longer occurs. Result (compare with before):
Screen.Recording.2026-03-13.at.12.32.44.AM.mov