- Why can't I use @Attribute(.unique) with CloudKit?
- Apple's SwiftData sync article says CloudKit cannot enforce the unique option, because changes sync at different times on different devices. The Core Data CloudKit page says unique constraints are not supported. Remove .unique (and #Unique) and handle duplicates in your own code after a sync, for example by merging records that share the same identifier.
- Do to-many relationships have to be optional too?
- Yes. Apple says CloudKit requires all relationships to be optional, and a non-optional array such as var items: [Item] = [] counts as required: Core Data reports it when the store loads. Write var items: [Item]? = [] and read it as items ?? [].
- Why does my property need a default value when init() already sets it?
- The value set in init() is not part of the model schema. Core Data's CloudKit setup checks the schema and refuses any property that is not optional and has no default value. Give the default where the property is declared, for example var name: String = "", or make it optional.
- Do I need inverse: on both sides of a relationship?
- No, name it on one side only. Apple says SwiftData sets the inverse when it can reliably infer it from your schema, and that you should set it explicitly otherwise. Inference fails when a model has more than one relationship to the same model, so naming it on one side of every pair avoids the doubt.
- Which capabilities does SwiftData need for iCloud sync?
- Two, per Apple: iCloud with CloudKit selected and a container chosen, and Background Modes with Remote notifications selected so CloudKit can tell the app about changes on the server. By default SwiftData uses the first CloudKit container in your entitlements; pass ModelConfiguration(cloudKitDatabase: .private("iCloud.…")) to choose one, or .none to keep the data on the device.
- Can I change my models after the app is released?
- Only by adding. Once the CloudKit schema is promoted to production, Apple says you cannot delete model types or change existing properties, and Core Data's page adds that record types and fields cannot be renamed. Add new properties with a default value or as optional, and leave old ones in place.