Post

Replies

Boosts

Views

Activity

Reply to Local SwiftData to CloudKit migration
I've got some news, which are discomforting: I can drive from local to cloudkit if I relaunch the app with changes, simulating multiple updates. In short, if I follow these passages, everything seems to work: Remove any cloudkit entitlement Migrate the local swiftData to a CloudKit-compatible schema Re-add the CloudKit entitlement Migrate the ModelConfiguration to use the preferred cloudKitDatabase This is definitely not something I can do, as I there must be a way to execute all these steps in one go? It also seems that a cloudKitDatabase: .none is not respected.
Jun ’24
Reply to Local SwiftData to CloudKit migration
let fromSchema = Schema(versionedSchema: ModelSchema_050624.self) let starterConfiguration = ModelConfiguration( nil, schema: fromSchema, isStoredInMemoryOnly: false, allowsSave: true, groupContainer: groupContainer, cloudKitDatabase: .none ) let modelContainer = try ModelContainer( for: Country.self, Episode.self, Movie.self, Season.self, Show.self, Network.self, NetworkSubscription.self, migrationPlan: AppModelMigrationPlan.self, configurations: starterConfiguration ) return modelContainer If the CloudKit entitlement is active (checked), this code throws: Thread 1: Fatal error: Could not initialize model container SwiftDataError(_error: SwiftData.SwiftDataError._Error.configurationSchemaNotFoundInContainerSchema) If the CloudKit entitlement is not active, this code migrates successfully.
Jun ’24
Reply to How to add multiple ModelConfigurations to a ModelContainer?
Have you tried to give a name to the configurations? This seems to work for me: let schema = Schema([ Item.self ]) let cloudSchema = Schema([CKItem.self]) let cloudConfiguration = ModelConfiguration("cloudConfig", schema: cloudSchema, isStoredInMemoryOnly: false, cloudKitDatabase: .private("iCloud.***.yyy.zzz")) let modelConfiguration = ModelConfiguration("localConfig", schema: schema, isStoredInMemoryOnly: false, cloudKitDatabase: .none) do { let allSchemas = Schema([Item.self, CKItem.self]) return try ModelContainer( for: allSchemas, configurations: [modelConfiguration, cloudConfiguration] ) If you open your simulator's Application Data folder, you'll find 2 stores: cloudConfig.store and localConfig.store, these are the names of the configurations above. Only one warning, there cannot be relations between the Schemas, otherwise they will be unified under the same schema. As an extra validation, Item uses an @Attribute(.unique), which is not valid on iCloud configurations.
Jul ’24
Reply to [Basics] Saving in SwiftData doesn't work
The stepper should act on the model that is stored in memory. Here you are inserting a new value, but it should increase the stepperValue in an entity of GroceryListItem. First you need to create an entity, and save it. let item = GroceryListItem() context.insert(item) Then you need to load that item, and use it in a binding to directly bind the stepper to its value. This could become your list of elements: List { ForEach(items) { item in @Bindable var bindingItem = item NavigationLink { // Destination view, a simple Stepper. Stepper(value: $bindingItem.stepperValue) { Text("\(item.stepperValue)") } } label: { Text("Item with value: \(item.stepperValue)") } } } onChange is not needed for this. :)
Jul ’24