Core Data, CloudKit - Deduplication causes nil relationships

I followed along apples Article for relevant store changes, mainly for data deduplication. https://developer.apple.com/documentation/coredata/consuming_relevant_store_changes

I also downloaded the Core Data / CloudKit Demo App which already has a deduplication process. https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud

In the Demo project I observed that more often than not, Posts loose their relationship to Tags.

After some investigation I assume that this happens, when a Tag which has a relationship to a Post, gets deleted during the deduplication process, before the relevant Post was synced to the device. When the Post now arrives on the device, its related Tag Object does no longer exist. Therefore it's also not possible to find the retained, deduped Tag-Object which should be connected to the Post.

I'm wondering why this was implemented that way in the Demo Project, as this really causes critical data loss.

I have also no Idea how to avoid it. In the Article, Apple recommends to use Core Datas tombstone to preserve some values of deleted objects. However, there is no further explaination.

It's also not implemented in the Demo project.

How do I restore lost relationships and how does the tombstone help with it?

Example:

Before it synced:

After it synced:

In their sample project Sharing Core Data objects between iCloud users, Apple now covers this situation. They added some code to handle the soft deletion of objects that are duplicates. To do so, they added a deduplicatedDate property on the Tag entity, then only remove the objects after some time. See the file PersistenceController+Deduplicate.swift. They also have to filter these objects marked as deduplicated from the UI (like in the TaggingView.swift).

Core Data, CloudKit - Deduplication causes nil relationships
 
 
Q