SwiftData

RSS for tag

SwiftData is an all-new framework for managing data within your apps. Models are described using regular Swift code, without the need for custom editors.

Posts under SwiftData tag

200 Posts

Post

Replies

Boosts

Views

Activity

Using SwiftData with a local and CloudKit backed configuration at the same time
I'm trying to set up an application using SwiftData to have a number of models backed by a local datastore that's not synced to CloudKit, and another set of models that is. I was able to achieve this previously with Core Data using multiple NSPersistentStoreDescription instances. The set up code looks something like: do { let fullSchema = Schema([ UnsyncedModel.self, SyncedModel.self, ]) let localSchema = Schema([UnsyncedModel.self]) let localConfig = ModelConfiguration(schema: localSchema, cloudKitDatabase: .none) let remoteSchema = Schema([SyncedModel.self]) let remoteConfig = ModelConfiguration(schema: remoteSchema, cloudKitDatabase: .automatic) container = try ModelContainer(for: fullSchema, configurations: localConfig, remoteConfig) } catch { fatalError("Failed to configure SwiftData container.") } However, it doesn't seem to work as expected. If I remove the synced/remote schema and configuration then everything works fine, but the moment I add in the remote schema and configuration I get various different application crashes. Some examples below: A Core Data error occurred." UserInfo={Reason=Entity named:... not found for relationship named:..., Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData<...> Has anyone ever been able to get a similar setup to work using SwiftData?
3
0
305
1d
Shared modelContainer between DocumentGroup and WindowGroup
Hi, I am currently developing a document-based application with additional WindowGroup for macOS and have encountered a challenge related to document container management. Specifically, I need to open a windowGroup that shares the same container as the one used in the DocumentGroup. However, my current approach of using a global shared model container has led to unintended behavior: any new document created is linked to existing ones, and changes made in one document are reflected across all documents. To address this issue, I am looking for a solution that allows each newly created document to be individualized while still sharing the document container with all relevant WindowGroups that require access to the data it holds. I would greatly appreciate any insights or recommendations you might have on how to achieve this. struct Todo: App { var body: some Scene { DocumentGroup(editing: Item.self, contentType: .item) { ContentView() } WindowGroup(for: Item.self) { $item in ItemView(item:$item) .modelContainer(Of DocumentGroup above) } } } Thank you for your time and assistance. Best regards,
1
1
106
3d
SwiftData ModelActor causes 5-10 second UI freeze when opening sheet
I'm experiencing a significant UI freeze in my SwiftUI app that uses SwiftData with CloudKit sync. When users tap a button to present a sheet for the first time after app launch, the entire UI becomes unresponsive for 5-10 seconds. Subsequent sheet presentations work fine. App Architecture Service layer: An @Observable class marked with @MainActor that orchestrates operations Persistence layer: A @ModelActor class that handles SwiftData operations SwiftUI views: Using @Environment to access the service layer The structure looks like this: @Observable @MainActor final class MyServices { let persistence: DataPersistence init(modelContainer: ModelContainer) { self.persistence = DataPersistence(modelContainer: modelContainer) } func addItem(title: String) async { // Creates and saves an item through persistence layer } } @ModelActor actor DataPersistence { func saveItem(_ item: Item) async { // Save to SwiftData } } The app initializes the ModelContainer at the Scene level and passes it through the environment: @main struct MyApp: App { let container = ModelContainer(for: Item.self) var body: some Scene { WindowGroup { ContentView() .modelContainer(container) .environment(MyServices(modelContainer: container)) } } } The Problem When a user taps the "Add Item" button which presents a sheet: Button("Add Item") { showingAddSheet = true } .sheet(isPresented: $showingAddSheet) { AddItemView(onAdd: { title in await services.addItem(title: title) }) } The UI freezes completely for 5-10 seconds on first presentation. During this time: The button remains in pressed state No UI interactions work The app appears completely frozen After the freeze, the sheet opens and everything works normally This only happens on the first sheet presentation after app launch. I suspect it's related to SwiftData's ModelContext initialization happening on the main thread despite using @ModelActor, but I'm not sure why this would happen given that ModelActor should handle background execution. Environment iOS 18 SwiftData with CloudKit sync enabled Xcode 16 Swift 6 Has anyone experienced similar freezes with SwiftData and @ModelActor? Is there something wrong with how I'm structuring the initialization of these components? The documentation suggests @ModelActor should handle background operations automatically, but the freeze suggests otherwise. Any insights would be greatly appreciated!
2
0
78
3d
SwiftData Fatal error
I'm developing an app that uses CloudKit synchronization with SwiftData and on visionOS I added an App Settings bundle. I have noticed that sometimes, when the app is open and the user changes a setting from the App Settings bundle, the following fatal error occurs: SwiftData/BackingData.swift:831: Fatal error: This model instance was destroyed by calling ModelContext.reset and is no longer usable. The setting is read within the App struct in the visionOS app target using @AppStorage and this value is in turn used to set the passthrough video dimming via the .preferredSurroundingsEffect modifier. The setting allows the user to specify the dimming level as dark, semi dark, or ultra dark. The fatal error appears to occur intermittently although the first time it was observed was after adding the settings bundle. As such, I suspect there is some connection between those code changes and this fatal error even though they do not directly relate to SwiftData.
1
0
164
4d
Error accessing backing data on deleted item in detached task
I have been working on an app for the past few months, and one issue that I have encountered a few times is an error where quick subsequent deletions cause issues with detached tasks that are triggered from some user actions. Inside a Task.detached, I am building an isolated model context, querying for LineItems, then iterating over those items. The crash happens when accessing a Transaction property through a relationship. var byTransactionId: [UUID: [LineItem]] { return Dictionary(grouping: self) { item in item.transaction?.id ?? UUID() } } In this case, the transaction has been deleted, but the relationship existed when the fetch occurred, so the transaction value is non-nil. The crash occurs when accessing the id. This is the error. SwiftData/BackingData.swift:1035: Fatal error: This model instance was invalidated because its backing data could no longer be found the store. PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0xb43fea2c4bc3b3f5 &lt;x-coredata://A9EFB8E3-CB47-48B2-A7C4-6EEA25D27E2E/Transaction/p1756&gt;))) I see other posts about this error and am exploring some suggestions, but if anyone has any thoughts, they would be appreciated.
1
0
122
4d
How to handle required @relationship optionals in SwiftData CloudKit?
Hi all, As you know, when using SwiftData Cloudkit, all relationships are required to be optional. In my app, which is a list app, I have a model class Project that contains an array of Subproject model objects. A Subproject also contains an array of another type of model class and this chain goes on and on. In this type of pattern, it becomes really taxxing to handle the optionals the correct way, i.e. unwrap them as late as possible and display an error to the user if unable to. It seems like most developers don't even bother, they just wrap the array in a computed property that returns an empty array if nil. I'm just wondering what is the recommended way by Apple to handle these optionals. I'm not really familiar with how the CloudKit backend works, but if you have a simple list app that only saves to the users private iCloud, can I just handwave the optionals like so many do? Is it only big data apps that need to worry? Or should we always strive to handle them the correct way? If that's the case, why does it seem like most people skip over them? Be great if an Apple engineer could weigh in.
3
0
107
5d
Issue with SwiftData inheritance
Every time I insert a subclass (MYShapeLayer) into the model context, the app crashes with an error: DesignerPlayground crashed due to fatalError in BackingData.swift at line 908. Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0xb2dbc55f3f4c57f2 <x-coredata://B1E3206B-40DE-4185-BC65-4540B4705B40/MYShapeLayer/p1>))) with Optional(A6CA4F89-107F-4A66-BC49-DD7DAC689F77) struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var designs: [MYDesign] var layers: [MYLayer] { designs.first?.layers ?? [] } var body: some View { NavigationStack { List { ForEach(layers) { layer in Text(layer.description) } } .onAppear { let design = MYDesign(title: "My Design") modelContext.insert(design) try? modelContext.save() } .toolbar { Menu("Add", systemImage: "plus") { Button(action: addTextLayer) { Text("Add Text Layer") } Button(action: addShapeLayer) { Text("Add Shape Layer") } } } } } private func addTextLayer() { if let design = designs.first { let newLayer = MYLayer(order: layers.count, kind: .text) newLayer.design = design modelContext.insert(newLayer) try? modelContext.save() } } private func addShapeLayer() { if let design = designs.first { let newLayer = MYShapeLayer(shapeName: "Ellipse", order: layers.count) newLayer.design = design modelContext.insert(newLayer) try? modelContext.save() } } } #Preview { ContentView() .modelContainer(for: [MYDesign.self, MYLayer.self, MYShapeLayer.self], inMemory: true) } @Model final class MYDesign { var title: String = "" @Relationship(deleteRule: .cascade, inverse: \MYLayer.design) var layers: [MYLayer] = [] init(title: String = "") { self.title = title } } @available(iOS 26.0, macOS 26.0, *) @Model class MYLayer { var design: MYDesign! var order: Int = 0 var title: String = "" init(order: Int = 0, title: String = "New Layer") { self.order = order self.title = title } } @available(iOS 26.0, macOS 26.0, *) @Model class MYShapeLayer: MYLayer { var shapeName: String = "" init(shapeName: String, order: Int = 0) { self.shapeName = shapeName super.init(order: order) } }
1
0
95
1w
iOS 26 SwiftData crash does not happen in iOS 16
I have a simple app that makes an HTTPS call to gather some JSON which I then parse and add to my SwiftData database. The app then uses a simple @Query in a view to get the data into a list. on iOS 16 this works fine. No problems. But the same code on iOS 26 (targeting iOS 18.5) crashes after about 15 seconds of idle time after the list is populated. The error message is: Could not cast value of type '__NSCFNumber' (0x1f31ee568) to 'NSString' (0x1f31ec718). and occurs when trying to access ANY property of the list. I have a stripped down version of the app that shows the crash available. To replicate the issue: open the project in Xcode 26 target any iOS 26 device or simulator compile and run the project. after the list is displayed, wait about 15 seconds and the app crashes. It is also of note that if you try to run the app again, it will crash immediately, unless you delete the app from the device. Any help on this would be appreciated. Feedback number FB20295815 includes .zip file Below is the basic code (without the data models) The Best Seller List.Swift import SwiftUI import SwiftData @main struct Best_Seller_ListApp: App { var body: some Scene { WindowGroup { ContentView() } .modelContainer (for: NYTOverviewResponse.self) } } ContentView.Swift import os.log import SwiftUI struct ContentView: View { @Environment(\.modelContext) var modelContext @State private var listEncodedName = String() var body: some View { NavigationStack () { ListsView() } .task { await getBestSellerLists() } } func getBestSellerLists() async { guard let url = URL(string: "https://api.nytimes.com/svc/books/v3/lists/overview.json?api-key=\(NYT_API_KEY)") else { Logger.errorLog.error("Invalid URL") return } do { let decoder = JSONDecoder() var decodedResponse = NYTOverviewResponse() //decode the JSON let (data, _) = try await URLSession.shared.data(from: url) decoder.keyDecodingStrategy = .convertFromSnakeCase decodedResponse = try decoder.decode(NYTOverviewResponse.self, from: data) //remove any lists that don't have list_name_encoded. Fixes a bug in the data decodedResponse.results!.lists = decodedResponse.results!.lists!.filter { $0.listNameEncoded != "" } // sort the lists decodedResponse.results!.lists!.sort { (lhs, rhs) -> Bool in lhs.displayName < rhs.displayName } //delete any potential existing data try modelContext.delete(model: NYTOverviewResponse.self) //add the new data modelContext.insert(decodedResponse) } catch { Logger.errorLog.error("\(error.localizedDescription)") } } } ListsView.Swift import os.log import SwiftData import SwiftUI @MainActor struct ListsView: View { //MARK: - Variables and Constants @Query var nytOverviewResponses: [NYTOverviewResponse] enum Updated: String { case weekly = "WEEKLY" case monthly = "MONTHLY" } //MARK: - Main View var body: some View { List { if nytOverviewResponses.isEmpty { ContentUnavailableView("No lists yet", systemImage: "list.bullet", description: Text("NYT Bestseller lists not downloaded yet")) } else { WeeklySection MonthlySection } } .navigationBarTitle("Bestseller Lists", displayMode: .large) .listStyle(.grouped) } var WeeklySection: some View { let rawLists = nytOverviewResponses.last?.results?.lists ?? [] // Build a value-typed array to avoid SwiftData faulting during sort let weekly = rawLists .filter { $0.updateFrequency == Updated.weekly.rawValue } .map { (name: $0.displayName, encoded: $0.listNameEncoded, model: $0) } .sorted { $0.name < $1.name } return Section(header: Text("Weekly lists to be published on \(nytOverviewResponses.last?.results?.publishedDate ?? "-")")) { ForEach(weekly, id: \.encoded) { item in Text(item.name).font(Font.custom("Georgia", size: 17)) } } } var MonthlySection: some View { let rawLists = nytOverviewResponses.last?.results?.lists ?? [] // Build a value-typed array to avoid SwiftData faulting during sort let monthly = rawLists .filter { $0.updateFrequency == Updated.monthly.rawValue } .map { (name: $0.displayName, encoded: $0.listNameEncoded, model: $0) } .sorted { $0.name < $1.name } return Section(header: Text("Monthly lists to be published on \(nytOverviewResponses.last?.results?.publishedDate ?? "-")")) { ForEach(monthly, id: \.encoded) { item in Text(item.name).font(Font.custom("Georgia", size: 17)) } } } }
4
0
153
1w
@Attribute 'unique' and complex keys
The 'unique' attribute is a really nice feature, BUT. In some of my apps, the unique identifier for an object is a combination of multiple attributes. (Example: a book title is not unique, but a combination of book title and author list is.) How do I model this with SwiftData? I cannot use @Attribute(.unique) on either the title OR the author list, but I want SwiftData to provide the same "insert or update" logic. Is this possible?
5
4
2.6k
2w
SwiftData ModelCoders.swift:1069 Unable to decode
Hello Developer Support, I have the following code and the following crash. How can the swift data model unable to decode and yet able to display the decoded value at the same time?! What's missing here? CODE: @Model final class DisplayCache { init(point: MKMapPoint) { self.point = point } var point: MKMapPoint } CRASH SwiftData/ModelCoders.swift:1069: Fatal error: Unable to decode this value MKMapPoint(x: 74358466.66307731, y: 97927933.41833577)
3
1
87
2w
Mutating an array of model objects that is a child of a model object
Hi all, In my SwiftUI / SwiftData / Cloudkit app which is a series of lists, I have a model object called Project which contains an array of model objects called subprojects: final class Project1 { var name: String = "" @Relationship(deleteRule: .cascade, inverse: \Subproject.project) var subprojects : [Subproject]? init(name: String) { self.name = name self.subprojects = [] } } The user will select a project from a list, which will generate a list of subprojects in another list, and if they select a subproject, it will generate a list categories and if the user selects a category it will generate another list of child objects owned by category and on and on. This is the pattern in my app, I'm constantly passing arrays of model objects that are the children of other model objects throughout the program, and I need the user to be able to add and remove things from them. My initial approach was to pass these arrays as bindings so that I'd be able to mutate them. This worked for the most part but there were two problems: it was a lot of custom binding code and when I had to unwrap these bindings using init?(_ base: Binding<Value?>), my program would crash if one of these arrays became nil (it's some weird quirk of that init that I don't understand at al). As I'm still learning the framework, I had not realized that the @model macro had automatically made my model objects observable, so I decided to remove the bindings and simply pass the arrays by reference, and while it seems these references will carry the most up to date version of the array, you cannot mutate them unless you have access to the parent and mutate it like such: project.subcategories?.removeAll { $0 == subcategory } project.subcategories?.append(subcategory) This is weirding me out because you can't unwrap subcategories before you try to mutate the array, it has to be done like above. In my code, I like to unwrap all optionals at the moment that I need the values stored in them and if not, I like to post an error to the user. Isn't that the point of optionals? So I don't understand why it's like this and ultimately am wondering if I'm using the correct design pattern for what I'm trying to accomplish or if I'm missing something? Any input would be much appreciated! Also, I do have a small MRE project if the explanation above wasn't clear enough, but I was unable to paste in here (too long), attach the zip or paste a link to Google Drive. Open to sharing it if anyone can tell me the best way to do so. Thanks!
5
0
161
2w
Inheritance in SwiftData — Fatal error: Never access a full future backing data
I'm implementing SwiftData with inheritance in an app. I have an Entity class with a property name. This class is inherited by two other classes: Store and Person. The Entity model has a one-to-many relationship with a Transaction class. I can list all my Entity models in a List with a @Query annotation without a problem. However, then I try to access the name property of an Entity from a Transaction relationship, the app crashes with the following error: Thread 1: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0x96530ce28d41eb63 <x-coredata://DABFF7BB-C412-474E-AD50-A1F30AC6DBE9/Person/p4>))) with Optional(F07E7E23-F8F0-4CC0-B282-270B5EDDC7F3) From my attempts to fix the issue, I noticed that: The crash seems related to the relationships with classes that has inherit from another class, since it only happens there. When I create new data, I can usually access it without any problem. The crash mostly happens after reloading the app. This error has been mentioned on the forum (for example here), but in a context not related with inheritance. You can find the full code here. For reference, my models looks like this: @Model class Transaction { @Attribute(.unique) var id: String var name: String var date: Date var amount: Double var entity: Entity? var store: Store? { entity as? Store } var person: Person? { entity as? Person } init( id: String = UUID().uuidString, name: String, amount: Double, date: Date = .now, entity: Entity? = nil, ) { self.id = id self.name = name self.amount = amount self.date = date self.entity = entity } } @Model class Entity: Identifiable { @Attribute(.preserveValueOnDeletion) var name: String var lastUsedAt: Date @Relationship(deleteRule: .cascade, inverse: \Transaction.entity) var operations: [Transaction] init( name: String, lastUsedAt: Date = .now, operations: [Transaction] = [], ) { self.name = name self.lastUsedAt = lastUsedAt self.operations = operations } } @available(iOS 26, *) @Model class Store: Entity { @Attribute(.unique) var id: String var locations: [Location] init( id: String = UUID().uuidString, name: String, lastUsedAt: Date = .now, locations: [Location] = [], operations: [Transaction] = [] ) { self.locations = locations self.id = id super.init(name: name, lastUsedAt: lastUsedAt, operations: operations) } } In order to reproduce the error: Run the app in the simulator. Click the + button to create a new transaction. Relaunch the app, then click on any transaction. The app crashes when it tries to read te name property while building the details view.
1
0
191
3w
SwiftData & CloudKit: Arrays of Codable Structs Causing NSKeyedUnarchiveFromData Error
I have SwiftData models containing arrays of Codable structs that worked fine before adding CloudKit capability. I believe they are the reason I started seeing errors after enabling CloudKit. Example model: @Model final class ProtocolMedication { var times: [SchedulingTime] = [] // SchedulingTime is Codable // other properties... } After enabling CloudKit, I get this error logged to the console: 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release CloudKit Console shows this times data as "plain text" instead of "bplist" format. Other struct/enum properties display correctly (I think) as "bplist" in CloudKit Console. The local SwiftData storage handled these arrays fine - this issue only appeared with CloudKit integration. What's the recommended approach for storing arrays of Codable structs in SwiftData models that sync with CloudKit?
5
1
201
3w
SwiftData with CloudKit in Widgets
Good morning everyone! Today I have a question about using SwiftData with CloudKit and Widgets. I recently set up my project for SwiftData and CloudKit synchronization, but for some reason, I’m not able to give my Widget access to this data. CloudKit works perfectly fine for my main app, but the Widget only shows placeholder data(the placeholder data which were defined in the get functions as catch, this is sure). I have set the CloudKit capability for my Widget extension and tried fetching data with the get-functions in the code below. I also ensured that the data model files are members of the Widget extension target and that the Widget extension uses the same CloudKit container as the main app. I wondered if it is possible and reasonable to save a copy of my CloudKit data in an App Group container, but in that case, the information shown in the Widget are not always up-to-date, so a solution that fetches data directly from CloudKit would be better. Has anyone had experience with this case? I couldn’t find much information about this problem online. In the code below, many parts have been deleted or altered because they are not relevant to the problem, as they don’t fetch data. The variables, functions, and data models in the code may sometimes have German names, but I hope you can still understand it. Thanks for your help! struct Provider: AppIntentTimelineProvider { //[Placeholder and snapshot] func timeline(for configuration: ConfigurationAppIntent, in context: Context) async -> Timeline<CleverEntry> { let entry = await loadAllVariables() return Timeline(entries: [entry], policy: .after(Date().addingTimeInterval(60 * 5))) } @MainActor private func getExam() -> [PruefungM] { //Old, local version /* guard let modelContainer = try? ModelContainer(for: PruefungM.self) else { return [] } let descriptor = FetchDescriptor<PruefungM>() let PRF = try? modelContainer.mainContext.fetch(descriptor) return PRF ?? [] */ do { let configuration = ModelConfiguration(cloudKitDatabase: .private("iCloud.my_bundle_id")) let container = try ModelContainer( for: PruefungM.self, configurations: configuration ) let descriptor = FetchDescriptor<PruefungM>() return try container.mainContext.fetch(descriptor) } catch { print("❌ Error(CloudKit): \(error)") return [] } } @MainActor private func getHAF() -> [HausaufgabeM] { do { let configuration = ModelConfiguration(cloudKitDatabase: .private("iCloud.my_bundle_id")) let container = try ModelContainer( for: HausaufgabeM.self, configurations: configuration ) let descriptor = FetchDescriptor<HausaufgabeM>() return try container.mainContext.fetch(descriptor) } catch { print("❌ Error (CloudKit): \(error)") return [] } } @MainActor private func loadAllVariables() -> CleverEntry { print("Function started") let HAF = getHAF() let PRF = getExam() //handling and returning the data } }
1
0
136
3w
Interactive Widget with SwiftData Not Triggering iCloud Sync
Hello everyone, I'm developing an app for iOS 18 using SwiftData, with iCloud synchronization enabled. My app also includes an interactive widget that allows users to mark tasks as complete, similar to Apple's Reminders widget. I'm facing a specific issue with how iCloud sync is triggered when changes are made from the widget. My Setup: Xcode 16 Swift 6 / iOS 18 SwiftData with iCloud Sync enabled. An interactive widget using App Intents to modify the SwiftData model. What's working correctly: App to Widget (Same Device): If I mark a task as complete in the main app, the widget on the same device updates instantly. Widget to App (Same Device): If I mark a task as complete using the interactive widget, the main app on the same device reflects this change immediately. App to App (Across Devices): If I make a change in the app on my iPhone, it syncs correctly via iCloud and appears in the app on my iPad. The Problem: The synchronization issue occurs specifically when an action is initiated from the widget and needs to be reflected on other devices, or when a change from another device needs to be reflected in the widget. Widget Change Not Syncing to Other Devices: If I mark a task as complete in the widget on my iPhone, the change does not sync to my iPad. The task on the iPad only updates after I manually open the main app on the iPhone. Remote Change Not Syncing to Widget: Similarly, if I mark a task as complete in the app on my iPad, the widget on my iPhone does not update to show this change. The widget only refreshes with the correct state after I open the main app on the iPhone. It seems that the widget's AppIntent correctly modifies the local SwiftData store, but this action isn't triggering the necessary background process to push the changes to iCloud. The sync only seems to happen when the main app, with its active ModelContainer, is brought to the foreground. My goal is for any change made in the widget to be reflected across all of the user's devices in near real-time, without requiring them to launch the main app to initiate the sync. Is there a specific API I need to call from my AppIntent to force a SwiftData sync, or a project capability I might be missing to allow the widget extension to trigger iCloud pushes? Any help or guidance would be greatly appreciated. Thank you!
1
0
92
3w
SwiftData and CloudKit: NSKeyedUnarchiveFromData Error
I just made a small test app that uses SwiftData with CloudKit capability. I created a simple Book model as seen below. It looks like enums and structs when used with CloudKit capability all trigger this error: 'NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release I fixed the error by using genreRaw String and using a computed property to use it in the app, but it popped back up after adding the ReadingProgress struct Should I ignore the error and assume Apple still supports enums and codable structs when using SwiftData with CloudKit? import SwiftData @Model class Book { var title: String = "" var author: String = "" var genreRaw: String = Genre.fantasy.rawValue var review: String = "" var rating: Int = 3 var progress: ReadingProgress? var genre: Genre { get { Genre(rawValue: genreRaw) ?? Genre.fantasy } set { genreRaw = newValue.rawValue } } init(title: String, author: String, genre: Genre, review: String, rating: Int, progress: ReadingProgress? = nil) { self.title = title self.author = author self.genre = genre self.review = review self.rating = rating self.progress = progress } } struct ReadingProgress: Codable { var currentPage: Int var totalPages: Int var isFinished: Bool var percentComplete: Double { guard totalPages > 0 else { return 0 } return Double(currentPage) / Double(totalPages) * 100 } } enum Genre: String, Codable, CaseIterable { case fantasy case scienceFiction case mystery case romance var displayName: String { switch self { case .fantasy: return "Fantasy" case .scienceFiction: return "Science Fiction" case .mystery: return "Mystery" case .romance: return "Romance" } } }
1
1
270
4w
Modelactors, Repository and bootloader
In iOS 26, should we have bootloader that runs the repo on startup - or should we have that inside tasks in root view? we have repos that runs as a «closed» functions, we dont throw but updates swiftdata and we use @query in the views. So what is best? and for the repo we should have a repo that runs the upserts manage relations eg? Should that run on a modelactor?
0
0
176
Sep ’25
SwiftUI TextField input is super laggy for SwiftData object
have a SwiftUI View where I can edit financial transaction information. The data is stored in SwiftData. If I enter a TextField element and start typing, it is super laggy and there are hangs of 1-2 seconds between each input (identical behaviour if debugger is detached). On the same view I have another TextField that is just attached to a @State variable of that view and TextField updates of that value work flawlessly. So somehow the hangs must be related to my SwiftData object but I cannot figure out why. This used to work fine until a few months ago and then I could see the performance degrading. I have noticed that when I use a placeholder variable like @State private var transactionSubject: String = "" and link that to the TextField, the performance is back to normal. I am then using .onSubmit { self.transaction.subject = self.transactionSubject } to update the value in the end but this again causes a 1 s hang. :/ Below the original code sample with some unnecessary stuff removed: struct EditTransactionView: View { @Environment(\.modelContext) var modelContext @Environment(\.dismiss) var dismiss @State private var testValue: String = "" @Bindable var transaction: Transaction init(transaction: Transaction) { self.transaction = transaction let transactionID = transaction.transactionID let parentTransactionID = transaction.transactionMasterID _childTransactions = Query(filter: #Predicate<Transaction> {item in item.transactionMasterID == transactionID }, sort: \Transaction.date, order: .reverse) _parentTransactions = Query(filter: #Predicate<Transaction> {item in item.transactionID == parentTransactionID }, sort: \Transaction.date, order: .reverse) print(_parentTransactions) } //Function to keep text length in limits func limitText(_ upper: Int) { if self.transaction.icon.count > upper { self.transaction.icon = String(self.transaction.icon.prefix(upper)) } } var body: some View { ZStack { Form{ Section{ //this one hangs TextField("Amount", value: $transaction.amount, format: .currency(code: Locale.current.currency?.identifier ?? "USD")) //this one works perfectly TextField("Test", text: $testValue) HStack{ TextField("Enter subject", text: $transaction.subject) .onAppear(perform: { UITextField.appearance().clearButtonMode = .whileEditing }) Divider() TextField("Select icon", text: $transaction.icon) .keyboardType(.init(rawValue: 124)!) .multilineTextAlignment(.trailing) } } } .onDisappear(){ if transaction.amount == 0 { // modelContext.delete(transaction) } } .onChange(of: selectedItem, loadPhoto) .navigationTitle("Transaction") .navigationBarTitleDisplayMode(.inline) .toolbar{ Button("Cancel", systemImage: "trash"){ modelContext.delete(transaction) dismiss() } } .sheet(isPresented: $showingImagePickerView){ ImagePickerView(isPresented: $showingImagePickerView, image: $image, sourceType: .camera) } .onChange(of: image){ let data = image?.pngData() if !(data?.isEmpty ?? false) { transaction.photo = data } } .onAppear(){ cameraManager.requestPermission() setDefaultVendor() setDefaultCategory() setDefaultGroup() } .sheet(isPresented: $showingAmountEntryView){ AmountEntryView(amount: $transaction.amount) } } } }
2
0
129
Sep ’25
SwiftData - Cloudkit stopped syncing
I have an app that from day 1 has used Swiftdata and successfully sync'd across devices with Cloudkit. I have added models to the data in the past and deployed the schema and it continued to sync across devices. Sometime I think in June.2025 I added a new model and built out the UI to display and manage it. I pushed a version to Test Flight (twice over a matter of 2 versions and a couple of weeks) and created objects in the new model in Test Flight versions of the app which should push the info to Cloudkit to update the schema. When I go to deploy the schema though there are no changes. I confirmed in the app that Cloudkit is selected and it's point to the correct container. And when I look in Cloudkit the new model isn't listed as an indes. I've pushed deploy schema changes anyway (more than once) and now the app isn't sync-ing across devices at all (even the pre-existing models aren't sync-ing across devices). I even submitted the first updated version to the app store and it was approved and released. I created objects in the new model in production which I know doesn't create the indexes in the development environment. But this new model functions literally everywhere except Cloudkit and I don't know what else to do to trigger an update.
3
1
161
Sep ’25