CloudKit sync stopped working with error „You can't save and delete the same record"

Hi there

We're using CloudKit in our app which, generally, syncs data perfectly between devices. However, recently the sync has stopped working (some changes will never sync and the sync is delayed for several days even with the app open on all devices). CloudKit's logs show the error „You can't save and delete the same record" and „Already have a mirrored relationship registered for this key", etc. We’ve a hunch that this issue is related to a mirrored relationship of one database entity.

Our scenario:

  • We've subclassed the database entities.
  • The database model (which we can't share publicly) contains mirrored relationships.
  • We store very long texts in the database (similar to a Word document that contains markup data – in case that’s relevant).
  • Deleting all data and starting with a completely new container and bundle identifier didn’t help (we tried that multiple times).
  • This issue occurs on macOS (15.2(24C101) as well on iOS (18.2).

Any hints on how to get the sync working again? Should we simply avoid mirrored relationships?

Many thanks

"You can't save and delete the same record"

Assuming that you are using Data + CloudKit, I am guessing that this can be triggered by your code and data. Consider the following configuration as an example:

  1. Delete Rule: cascade
  2. articleA.associatedArticles -> [articleB, articleC...]
  3. articleB.associatedArticles -> [articleA, articleC...]

If you delete articleA, Core Data will delete articleB due to 1 and 2, and change articleB due to 3 at the same time. You can review your code to see if the above case can happen.

When you use Core Data + CloudKit, NSPersistentCloudKitContainer configures and performs the CloudKit operations without any involvement of your code, and so it is worth filing a feedback report for the Core Data team to investigate why the case isn't handled – If you do so, please share your report ID here.

"Already have a mirrored relationship registered for this key"

Based on the (more) information you provided to DTS, this seems to indicate that you has a reflexive relationship (a relationship whose source and destination point to the same entity), and NSPersistentCloudKitContainer has trouble to mirror the relationship to the CloudKit schema.

Does your Core Data model indeed has the reflexive relationship? If yes, has it been there for long time, or did you add that recently before you saw the error? At the time I played NSPersistentCloudKitContainer, a reflexive relationship was supported, and so I will be surprised if that is the culprit.

The database model (which we can't share publicly) contains mirrored relationships.

Does "mirrored relationships" mean "reflexive relationships" here? If not, please share the definition so we are on the same page.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for the reply!

Firstly, correct, we're using CoreData + CloudKit and "mirrored relationships" means "reflexive relationships" (e.g., Article.similarArticles whereby both are of the type Article).

Assuming that you are using Data + CloudKit, I am guessing that this can be triggered by your code and data.

Regrettably, that doesn't seem to be the case, also given the Delete Rule is "Nullify". Digging through our old messages, we found out that we had already reported this very same issue a couple of years ago with a similar data model but where no entity was ever deleted. At that time we filed feedback report 9118745 and we were told that this was likely a bug.

it is worth filing a feedback report for the Core Data team

Given this seems to be the very same issue, I’ve added the information of this post to our above ticket (and have provided the relevant system diagnose logs, data model, etc. there too).

Does your Core Data model indeed has the reflexive relationship? If yes, has it been there for long time, or did you add that recently before you saw the error?

Yes, we've added this reflexive relationship to a completely new project/iCloud container in 2024 and the reflexive relationship indeed synced, however, not in all instances. So it looks like NSPersistentCloudKitContainer indeed supports reflexive relationships, but is somehow confused and fails only in certain instances.

Many thanks

Thanks for providing more context, and also the feedback report ID (FB9118745). That does sound like a corner issue on the framework side. I don't see anything that can work around it unfortunately...

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

CloudKit sync stopped working with error „You can't save and delete the same record"
 
 
Q