Search results for

“SwiftData inheritance relationship”

4,986 results found

Post

Replies

Boosts

Views

Activity

Handling Context Save Errors
I was wondering if errors are common for the code below for saving SwiftData data and what would be the best way to handle them (popup, closing the app)? do { try modelContext.save() } catch { print(error) }
Topic: Design SubTopic: General Tags:
1
0
286
Jan ’26
@ComputedProperty vs copying values SwiftData AppEntity
I'm setting up App Entities for my SwiftData models and I'm not sure about the best way to reference SwiftData model properties in the AppEntity. I have a SwiftData model with many properties: @Model final class Contact { @Attribute(.unique) var id: UUID = UUID() var name: String var phoneNumber: String var email: String var website: URL? var birthday: Date? var notes: String // ... many more properties } I want to expose these properties on my AppEntity so they're available for system features, such as giving Apple Intelligence more context about on-screen content. struct ContactEntity: AppEntity { var id: UUID @Property(title: Name) var name: String @Property(title: Phone) var phoneNumber: String @Property(title: Email) var email: String // ... all the other properties } I couldn't find guidance in the documentation for this specific situation. I've considered two approaches: Add @Property variables to the AppEntity for each SwiftData model property and copy all values fr
1
0
149
Jan ’26
Deleting Production Database SwiftData
Hi all, I have setup my app to use SwiftData with CloudKit sync. I have a production environment and development environment. I can reset the development environment for myself and all users in CloudKit console, but I can't reset the production one as it's tried to users' iCloud accounts, so I've added a button in-app for that feature. In the onboarding of my app, I pre-seed the DB with some default objects, which should be persisted between app install. The issue I'm running into is that I'm unable to force-pull these models from iCloud during the onboarding of a clean re-install, which leads to the models later appearing as duplicates once the user has been on the app for a few minutes and it has pulled from their iCloud account. If anyone has any suggestions on how to handle this issue, I would greatly appreciate it.
2
0
273
Jan ’26
Accessory View Not Displayed When Switching Input Methods via Bluetooth Keyboard
Hello everyone, When I press Control + Space on my Bluetooth keyboard to trigger input method switching, the accessory view fails to appear. This prevents me from quickly identifying the current input method type. Upon inspecting the View Hierarchy, I noticed that UICursorAccessoryView is not being created. For context, my input method responder inherits from UIResponder and conforms to the UITextInputTraits, UIKeyInput, and UITextInput protocols. The accessory view displays normally during accented input and Chinese input. Could you please guide me on how to troubleshoot this issue?
4
0
534
Jan ’26
DNS Proxy system extension – OSSystemExtensionErrorDomain error 9 “validationFailed” on clean macOS machine
Hi, I’m implementing a macOS DNS Proxy as a system extension and running into a persistent activation error: OSSystemExtensionErrorDomain error 9 (validationFailed) with the message: extension category returned error This happens both on an MDM‑managed Mac and on a completely clean Mac (no MDM, fresh install). Setup macOS: 15.x (clean machine, no MDM) Xcode: 16.x Team ID: AAAAAAA111 (test) Host app bundle ID: com.example.agent.NetShieldProxy DNS Proxy system extension bundle ID: com.example.agent.NetShieldProxy.dnsProxy The DNS Proxy is implemented as a NetworkExtension system extension, not an app extension. Host app entitlements From codesign -d --entitlements :- /Applications/NetShieldProxy.app: xml com.apple.application-identifier AAAAAAA111.com.example.agent.NetShieldProxy com.apple.developer.system-extension.install com.apple.developer.team-identifier AAAAAAA111 com.apple.security.app-sandbox com.apple.security.application-groups group.com.example.NetShieldmac com.apple.security.files.user-selected.
9
0
420
Jan ’26
Reply to Slow rendering List backed by SwiftData @Query
So basically, could I build the Photos app (or something like it) in SwiftData. Seems to be no. Because Query cannot handle anything remotely close to the thousands of displayed objects. I tested a manual fetch on context. Performed in milliseconds as expected. But anything in Query, completely fails with like 10 seconds when it gets bad
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’26
Reply to Persisting User Settings with SwiftData
I'd like to comment the second question a bit, as it hasn't been addressed yet: Sometimes when running on a new device it will create a second UserSettings while it is waiting for the original one to sync from CloudKit. When you use SwiftData + CloudKit, under the hood, the framework uses NSPersistentCloudKitContainer to synchronize data. In that environment, duplicate data is sometimes inevitable, which is discussed in great details in the following section: Remove duplicate data The solution is to de-duplicate the data. How to do so is discussed and demonstrated in the sample code. Back to what to what you intent to achieve, my suggestion will be: If you plan to use SwiftData + CloudKit extensively in your app, you might need to handle duplicate data anyway. In this case, persisting user settings to your SwiftData store (and Synchronizing them via NSPersistentCloudKitContainer) makes a lot of sense. If the target is as simple as saving some Cloud-based user settings, I'd probably
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’26
Reply to Using Observations with SwiftData @Model
I am working on the real project. The changes to the list of @Model are still not being captured in the Observations. When I modify or add/remove from the list sometimes a property of the object that IS NOT changed shows as being changed. That property has exactly one setter call and is not linked to the edit, add, or remove code. When I tried to observe the list as a whole, I achieved a FATAL ERROR for illegal attempt to resolve a future for a relationship that doesn't have a cached value!
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’26
Reply to Persisting User Settings with SwiftData
Thank you for your post. After reviewing it, I have a suggestion for using UserDefaults instead, in my modest opinion and without knowing all your requirements. If you intend to utilize SwiftData for this purpose, I recommend querying for the single instance of the model and binding it to your view. To achieve this, query for a single instance where a unique attribute, such as a stored value, matches a specific value. Retrieve the stored ID and use it in your filter to ensure that only one instance of the model is fetched and used within the view. To facilitate future debugging, you may also consider deleting the existing model from CloudKit to eliminate any potential duplicates in your container. This can be done from the CloudKit Dashboard. While UserDefaults may not be the most efficient solution for your specific requirements, I would be interested in understanding the reasons behind your decision to use SwiftData. Additionally, I would appreciate any recommendations from other developer
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’26
Persisting User Settings with SwiftData
I was wondering what the recommended way is to persist user settings with SwiftData? It seems the SwiftData API is focused around querying for multiple objects, but what if you just want one UserSettings object that is persisted across devices say for example to store the user's age or sorting preferences. Do we just create one object and then query for it or is there a better way of doing this? Right now I am just creating: import SwiftData @Model final class UserSettings { var age: Int = 0 var sortAtoZ: Bool = true init(age: Int = 0, sortAtoZ: Bool = true) { self.age = age self.sortAtoZ = sortAtoZ } } In my view I am doing as follows: import SwiftUI import SwiftData struct SettingsView: View { @Environment(.modelContext) var context @Query var settings: [UserSettings] var body: some View { ForEach(settings) { setting in let bSetting = Bindable(setting) Toggle(Sort A-Z, isOn: bSetting.sortAtoZ) TextField(Age, value: bSetting.age, format: .number) } .onAppear { if settings.
3
0
521
Jan ’26
Reply to Best practice for centralizing SwiftData query logic and actions in an @Observable manager?
A SwiftData query (@Query) is currently tied to a SwiftUI view and relies on the modelContext environment value of the view. There is currently no way to use @Query outside of a SwiftUI view. To implement the query logics in a controller class, you basically need to to do the following with your own code: Fetch data using FetchDescriptor + ModelContext.fetch. Update the result set when relevant changes happen. To do step 2, assuming you are using the SwiftData default store, which is based on Core Data, you need to observe the notifications triggered by a store change (basically NSManagedObjectContextObjectsDidChange and .NSPersistentStoreRemoteChange), check if the change is relevant, and re-fetch the data set as needed. All these will be a bit involved, and yet have already implemented (as Query) on the framework side. I'd hence suggest that you file a feedback report to request an API that does what Query does but doesn't rely on SwiftUI – If you do so, please share your report ID here fo
Jan ’26
Reply to SwiftData @Model: Optional to-many relationship is never nil at runtime
SwiftData default store is based on Core Data. As a historical implementation detail, at runtime, a Core Data to-many relationship, once persisted, is always a [], and never a nil. To adapt this behavior in an effective way, SwiftData always initializes a nil to-many relationship with an empty collection at runtime. So to your first question, the behavior that optional to-many relationships in SwiftData are never nil at runtime is as-designed. The requirement that relationships must be optional is a way to express that NSPersistentCloudKitContainer does not handle relationships atomically, and is enforced by a schema validator at compile time. The validation doesn't have any impact at runtime, and so SwiftData to-many relationships being [] at runtime doesn't break the CloudKit synchronization. To use SwiftData + CloudKit, you make sure your model schema is compatible with the rules so the schema validator doesn
Jan ’26
Reply to Inability to Communicate via APDU on iOS Despite NFC Tag Detection
You can't switch sessions once connected, but you can connect to an NDEF tag by using NFCTagReaderSession and do both NDEF and non-NDEF actions. Because NFCISO7816Tag inherits from NFCNDEFTag it gains all its capabilities and then adds more specific functionality. If a NFCTag returned from a NFCTagReaderSession supports NDEF, NDEF operations (query NDEF status, write and read NDEF) are performed internally, and the NDEF data should be available as properties in the same NFCTagReaderSession.
Topic: App & System Services SubTopic: Core OS Tags:
Jan ’26
Handling Context Save Errors
I was wondering if errors are common for the code below for saving SwiftData data and what would be the best way to handle them (popup, closing the app)? do { try modelContext.save() } catch { print(error) }
Topic: Design SubTopic: General Tags:
Replies
1
Boosts
0
Views
286
Activity
Jan ’26
@ComputedProperty vs copying values SwiftData AppEntity
I'm setting up App Entities for my SwiftData models and I'm not sure about the best way to reference SwiftData model properties in the AppEntity. I have a SwiftData model with many properties: @Model final class Contact { @Attribute(.unique) var id: UUID = UUID() var name: String var phoneNumber: String var email: String var website: URL? var birthday: Date? var notes: String // ... many more properties } I want to expose these properties on my AppEntity so they're available for system features, such as giving Apple Intelligence more context about on-screen content. struct ContactEntity: AppEntity { var id: UUID @Property(title: Name) var name: String @Property(title: Phone) var phoneNumber: String @Property(title: Email) var email: String // ... all the other properties } I couldn't find guidance in the documentation for this specific situation. I've considered two approaches: Add @Property variables to the AppEntity for each SwiftData model property and copy all values fr
Replies
1
Boosts
0
Views
149
Activity
Jan ’26
Deleting Production Database SwiftData
Hi all, I have setup my app to use SwiftData with CloudKit sync. I have a production environment and development environment. I can reset the development environment for myself and all users in CloudKit console, but I can't reset the production one as it's tried to users' iCloud accounts, so I've added a button in-app for that feature. In the onboarding of my app, I pre-seed the DB with some default objects, which should be persisted between app install. The issue I'm running into is that I'm unable to force-pull these models from iCloud during the onboarding of a clean re-install, which leads to the models later appearing as duplicates once the user has been on the app for a few minutes and it has pulled from their iCloud account. If anyone has any suggestions on how to handle this issue, I would greatly appreciate it.
Replies
2
Boosts
0
Views
273
Activity
Jan ’26
Accessory View Not Displayed When Switching Input Methods via Bluetooth Keyboard
Hello everyone, When I press Control + Space on my Bluetooth keyboard to trigger input method switching, the accessory view fails to appear. This prevents me from quickly identifying the current input method type. Upon inspecting the View Hierarchy, I noticed that UICursorAccessoryView is not being created. For context, my input method responder inherits from UIResponder and conforms to the UITextInputTraits, UIKeyInput, and UITextInput protocols. The accessory view displays normally during accented input and Chinese input. Could you please guide me on how to troubleshoot this issue?
Replies
4
Boosts
0
Views
534
Activity
Jan ’26
DNS Proxy system extension – OSSystemExtensionErrorDomain error 9 “validationFailed” on clean macOS machine
Hi, I’m implementing a macOS DNS Proxy as a system extension and running into a persistent activation error: OSSystemExtensionErrorDomain error 9 (validationFailed) with the message: extension category returned error This happens both on an MDM‑managed Mac and on a completely clean Mac (no MDM, fresh install). Setup macOS: 15.x (clean machine, no MDM) Xcode: 16.x Team ID: AAAAAAA111 (test) Host app bundle ID: com.example.agent.NetShieldProxy DNS Proxy system extension bundle ID: com.example.agent.NetShieldProxy.dnsProxy The DNS Proxy is implemented as a NetworkExtension system extension, not an app extension. Host app entitlements From codesign -d --entitlements :- /Applications/NetShieldProxy.app: xml com.apple.application-identifier AAAAAAA111.com.example.agent.NetShieldProxy com.apple.developer.system-extension.install com.apple.developer.team-identifier AAAAAAA111 com.apple.security.app-sandbox com.apple.security.application-groups group.com.example.NetShieldmac com.apple.security.files.user-selected.
Replies
9
Boosts
0
Views
420
Activity
Jan ’26
Reply to SwiftUI TextEditor: replaced text jumps outside current selection
Hi Albert, thank you for replying. Here is my Richnote class: import SwiftUI import SwiftData @Model final class Richnote { @Attribute(.externalStorage) var content : AttributedString var createdAt: Date = Date() init(content: AttributedString) { self.content = content } }
Topic: UI Frameworks SubTopic: SwiftUI
Replies
Boosts
Views
Activity
Jan ’26
Reply to Slow rendering List backed by SwiftData @Query
So basically, could I build the Photos app (or something like it) in SwiftData. Seems to be no. Because Query cannot handle anything remotely close to the thousands of displayed objects. I tested a manual fetch on context. Performed in milliseconds as expected. But anything in Query, completely fails with like 10 seconds when it gets bad
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’26
Reply to Persisting User Settings with SwiftData
I'd like to comment the second question a bit, as it hasn't been addressed yet: Sometimes when running on a new device it will create a second UserSettings while it is waiting for the original one to sync from CloudKit. When you use SwiftData + CloudKit, under the hood, the framework uses NSPersistentCloudKitContainer to synchronize data. In that environment, duplicate data is sometimes inevitable, which is discussed in great details in the following section: Remove duplicate data The solution is to de-duplicate the data. How to do so is discussed and demonstrated in the sample code. Back to what to what you intent to achieve, my suggestion will be: If you plan to use SwiftData + CloudKit extensively in your app, you might need to handle duplicate data anyway. In this case, persisting user settings to your SwiftData store (and Synchronizing them via NSPersistentCloudKitContainer) makes a lot of sense. If the target is as simple as saving some Cloud-based user settings, I'd probably
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’26
Reply to Using Observations with SwiftData @Model
I am working on the real project. The changes to the list of @Model are still not being captured in the Observations. When I modify or add/remove from the list sometimes a property of the object that IS NOT changed shows as being changed. That property has exactly one setter call and is not linked to the edit, add, or remove code. When I tried to observe the list as a whole, I achieved a FATAL ERROR for illegal attempt to resolve a future for a relationship that doesn't have a cached value!
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jan ’26
Reply to Persisting User Settings with SwiftData
Thank you for your post. After reviewing it, I have a suggestion for using UserDefaults instead, in my modest opinion and without knowing all your requirements. If you intend to utilize SwiftData for this purpose, I recommend querying for the single instance of the model and binding it to your view. To achieve this, query for a single instance where a unique attribute, such as a stored value, matches a specific value. Retrieve the stored ID and use it in your filter to ensure that only one instance of the model is fetched and used within the view. To facilitate future debugging, you may also consider deleting the existing model from CloudKit to eliminate any potential duplicates in your container. This can be done from the CloudKit Dashboard. While UserDefaults may not be the most efficient solution for your specific requirements, I would be interested in understanding the reasons behind your decision to use SwiftData. Additionally, I would appreciate any recommendations from other developer
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jan ’26
Persisting User Settings with SwiftData
I was wondering what the recommended way is to persist user settings with SwiftData? It seems the SwiftData API is focused around querying for multiple objects, but what if you just want one UserSettings object that is persisted across devices say for example to store the user's age or sorting preferences. Do we just create one object and then query for it or is there a better way of doing this? Right now I am just creating: import SwiftData @Model final class UserSettings { var age: Int = 0 var sortAtoZ: Bool = true init(age: Int = 0, sortAtoZ: Bool = true) { self.age = age self.sortAtoZ = sortAtoZ } } In my view I am doing as follows: import SwiftUI import SwiftData struct SettingsView: View { @Environment(.modelContext) var context @Query var settings: [UserSettings] var body: some View { ForEach(settings) { setting in let bSetting = Bindable(setting) Toggle(Sort A-Z, isOn: bSetting.sortAtoZ) TextField(Age, value: bSetting.age, format: .number) } .onAppear { if settings.
Replies
3
Boosts
0
Views
521
Activity
Jan ’26
Reply to Best practice for centralizing SwiftData query logic and actions in an @Observable manager?
A SwiftData query (@Query) is currently tied to a SwiftUI view and relies on the modelContext environment value of the view. There is currently no way to use @Query outside of a SwiftUI view. To implement the query logics in a controller class, you basically need to to do the following with your own code: Fetch data using FetchDescriptor + ModelContext.fetch. Update the result set when relevant changes happen. To do step 2, assuming you are using the SwiftData default store, which is based on Core Data, you need to observe the notifications triggered by a store change (basically NSManagedObjectContextObjectsDidChange and .NSPersistentStoreRemoteChange), check if the change is relevant, and re-fetch the data set as needed. All these will be a bit involved, and yet have already implemented (as Query) on the framework side. I'd hence suggest that you file a feedback report to request an API that does what Query does but doesn't rely on SwiftUI – If you do so, please share your report ID here fo
Replies
Boosts
Views
Activity
Jan ’26
Reply to SwiftData @Model: Optional to-many relationship is never nil at runtime
SwiftData default store is based on Core Data. As a historical implementation detail, at runtime, a Core Data to-many relationship, once persisted, is always a [], and never a nil. To adapt this behavior in an effective way, SwiftData always initializes a nil to-many relationship with an empty collection at runtime. So to your first question, the behavior that optional to-many relationships in SwiftData are never nil at runtime is as-designed. The requirement that relationships must be optional is a way to express that NSPersistentCloudKitContainer does not handle relationships atomically, and is enforced by a schema validator at compile time. The validation doesn't have any impact at runtime, and so SwiftData to-many relationships being [] at runtime doesn't break the CloudKit synchronization. To use SwiftData + CloudKit, you make sure your model schema is compatible with the rules so the schema validator doesn
Replies
Boosts
Views
Activity
Jan ’26
Reply to Inability to Communicate via APDU on iOS Despite NFC Tag Detection
You can't switch sessions once connected, but you can connect to an NDEF tag by using NFCTagReaderSession and do both NDEF and non-NDEF actions. Because NFCISO7816Tag inherits from NFCNDEFTag it gains all its capabilities and then adds more specific functionality. If a NFCTag returned from a NFCTagReaderSession supports NDEF, NDEF operations (query NDEF status, write and read NDEF) are performed internally, and the NDEF data should be available as properties in the same NFCTagReaderSession.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Jan ’26
Reply to SwiftData Migration: Why no explicit ETL?
Sorry, ETL is Extract (which means to pull the data from the source system to another system for mapping Transform: Transform the data Load: Push transformed data to the target system Typical migration method. SwiftData migration does not follow this pattern.
Replies
Boosts
Views
Activity
Jan ’26