Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

Reply to XCode reverts CoreData's .xccurrentversion
That's interesting. I created this model in Version 16.3 (16E140), which I believe is the current stable XCode release. I created new models using Editor > Add Model Version.... I just tested creating another model, and the new one still contains usedWithSwiftData=YES. I don't need my models to be SwiftData compatible at this moment. So, do you think removing this field would resolve the issue?
Apr ’25
Reply to SwiftData and iCloud
I eventually solved the problem. I had a relationship, just the one, that I hadn't marked as optional. My tip to anyone else who has a similar problem is to make sure you run the app on the simulator rather than just directly on your phone as the debug information will give you a much clearer idea of what is going wrong. I'm just a hobbyist writing my first app so whereas proper developers would have known this, I didn't have a clue! My app is looking fabulous now though :)
Apr ’25
SwiftData crashes on fetchHistory
Hi, would it be possible that instead of crashing when calling fetchHistory that function simply throws an error instead? fetchHistory seems to crash when it cannot understand the models if they are not compatible etc… which is understandable, but it makes it really difficult to handle and debug, there's not a lot of details, and honestly I would just rather that it throws an error and let me ignore a history entry that might be useless rather than crashing the entire app. Thank you!
1
0
95
Apr ’25
Reply to Mac App Crashing with Illegal Instructions
After some digging I found out that SwiftData is causing the problem. More concrete relationships I have a class Account with has (optional) categories @Relationship(deleteRule: .cascade, inverse: Category.account) var _categories: [Category]? var categories: [Category] { self._categories ?? [] } in the class Category I have the a simple account variable var account: Account The application crahses when I use the following init function in Account (at the last line) init(id: Int, income: Bool, location: String, maintainance: Bool, special: Bool, taxRate: Float, tenant: String, title: String) { self.id = id self.income = income self.location = location self.maintainance = maintainance self.special = special self.taxRate = taxRate self.tenant = tenant self.title = title self._categories = [] } or when I use those lines // category not found, so create one let category: Category = Category(title: categoryName, account: self) if self._categories == nil { self._categories = [category] }
Apr ’25
Why does a Live Activity get .dismissed by the system even when all conditions seem normal?
Hello, I'm working with Live Activities and noticed that sometimes an activity transitions to .dismissed, even though the user hasn't manually swiped it away and system conditions appear to be completely normal — such as: The activity is still within its intended 8-hour lifetime The battery level is high The app is active or recently active The activity is lightweight (not using frequent updates) According to the ActivityKit documentation: /// The Live Activity ended and is no longer visible because a person or the system removed it. case dismissed This doesn’t clarify why the system would dismiss an activity when all conditions seem fine. Additional context: When reviewing system logs via Console.app, we’re seeing messages such as: liveactivitiesd Removing activity from replicator: 381F3DDC-585B-4021-B075-548606F543DA for relationship IDs: [C7AB9C2A-49DD-43FC-BB58-D768ECF9D354] This suggests that the system is actively removing the activity, but there’s no API or reason provided that helps us unders
1
0
235
Apr ’25
Can DocC be used to abstract out lines of comments?
I'd like to point to comments about my code from outside the coding file to keep the code easier to read, but still have the comments show in Xcode's Quick Help. I've grown to appreciate lengthier comments, including Examples for demonstrating values that explain what calculations are doing. My DocC formatted comments have quickly reach 25 lines and some would benefit from even more lines than that, which I do not want to bloat my code files with. I have tried to find a way to abstract these lines into a .docc file that references the given property, and the following markdown successfully shows this when I run Build Documentation. However, clicking on this property fails to show any comments in Quick Help, even though they did prior to my moving them to an .md file. Unfortunately, I'd prefer reading these in the future in Quick Help over Xcode's Developer Documentation Window. I haven't used markdown in decades, so I'm hoping I'm merely making a markdown mistake someone can find below. For reference, InvItem
2
0
134
Apr ’25
SwiftData assertionFailure crash in release builds?
I have an issue in my app, where the crashing frame is an assertionFailure in BackingData.set inside SwiftData framework. My own app doesn't appear until frame 14. I have no idea what causes this, or even how to create a reproducible project as this only happens on some devices. The frame prior to the assertionFailure is this: #1 (null) in BackingData.set(any:value:) () It seems like there is a backing data encoding happening in my Model class, and some value is causing it to fail. The model being accessed is through a relationship, and the frame in the app crashing is along the lines of Text(parent.child.name) Obviously, something is wrong in how I have made child, but the part that stand out to me is the assertionFailure in a release build
4
0
127
Apr ’25
SwiftData and iCloud
I'm a first time developer for Swift, (getting on a bit!) but after programming in VB back in the late 90s I wanted to write an app for iPhone. I think I might have gone about it the wrong way, but I've got an app that works great on my iPhone or works great on my iPad. It saves the data persistently on device, but, no matter how much I try, what I read and even resorting to AI (ChatGPT & Gemini) I still can't get it to save the data on iCloud to synchronise between the two and work across the devices. I think it must be something pretty fundamental I'm doing (or more likely not doing) that is causing the issue. I'm setting up my signing and capabilities as per the available instructions but I always get a fatal error. I think it might be something to do with making fields optional, but at this point I'm second guessing myself and feeling a complete failure. Any advice or pointers would be really gratefully appreciated. I like my app and would like eventually to get it on the App Store but at this point i
2
0
198
Apr ’25
Reply to @SectionedFetchRequest using relationship as sectionIdentifier
I was having the same issue and fixed it by adding at least one of the relationship's keys to the sort descriptors of the request. The issue is that the request is not pre-fetching the relationship key-path because it's not included in the request in any capacity). So it seems using it as a section identifier isn't sufficient. I'm assuming SectionedFetchRequest does grouping after the request has returned its results.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’25
Reply to SwiftData Predicates crashes when using Generic
Ran into this as well. The compiler is optimizing something incorrectly for release builds. Using Predicate init directly (instead of the Macro) doesn't resolve the issue. Nor does any combo of explicit/verbose casting. Something as simple as this: func fetchModel(with id: UUID, prefetching relationshipKeyPaths: [PartialKeyPath] = []) throws -> M where M: PersistentModel, M.ID == UUID { var fetchDescriptor = FetchDescriptor() fetchDescriptor.relationshipKeyPathsForPrefetching = relationshipKeyPaths fetchDescriptor.predicate = #Predicate { $0.id == id } fetchDescriptor.fetchLimit = 1 guard let model: M = try? modelContext.fetch(fetchDescriptor).first else { throw SwiftDataRepoError.modelNotFound(withId: id) } return model } Results in this crash: SwiftData/DataUtilities.swift:85: Fatal error: Couldn't find Person. on Person with fields [SwiftData.Schema.PropertyMetadata(name: id, keypath: Person., defaultValue: Optional(5CAE942A-EF4E-4B89-A777-D79153C7F276), metadata: nil) .... Only thing that work
Apr ’25
Question about BGAppRefreshTask approach for medication scheduling app
I'm developing a medication scheduling app similar to Apple Health's Medications feature, and I'd like some input on my current approach to background tasks. In my app, when a user creates a medication, I generate ScheduledDose objects (with corresponding local notifications) for the next 2 weeks and save them to SwiftData. To ensure this 2-week window stays current, I've implemented a BGAppRefreshTask that runs daily to generate new doses as needed. My concern is whether BGAppRefreshTask is the appropriate mechanism for this purpose. Since I'm not making any network requests but rather generating and storing local data, I'm questioning if this is the right approach. I'm also wondering how Apple Health's Medications feature handles this kind of scheduling. Their app seems to maintain future doses regardless of app usage patterns. Has anyone implemented something similar or can suggest the best background execution API for this type of scenario? Thanks for any guidance you can provide.
2
0
193
Apr ’25
SwiftData and @Query to find all records for the current date of a multidatepicker (Set = [])
I’m trying to build a CRUD app using SwiftData, @Query model and multidatepicker. The data from a multidatepicker is stored or persists in SwiftData as Set = []. My current dilemma is how to use SwiftData and @Query model Predicate to find all records on the current date. I can’t find any SwiftData documentation or examples @Query using Set = []. My CRUD app should retrieve all records for the current date. Unfortunately, I don’t know the correct @Query model syntax for Set = [].
0
0
76
Apr ’25
Reply to XCode reverts CoreData's .xccurrentversion
That's interesting. I created this model in Version 16.3 (16E140), which I believe is the current stable XCode release. I created new models using Editor > Add Model Version.... I just tested creating another model, and the new one still contains usedWithSwiftData=YES. I don't need my models to be SwiftData compatible at this moment. So, do you think removing this field would resolve the issue?
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData and iCloud
I eventually solved the problem. I had a relationship, just the one, that I hadn't marked as optional. My tip to anyone else who has a similar problem is to make sure you run the app on the simulator rather than just directly on your phone as the debug information will give you a much clearer idea of what is going wrong. I'm just a hobbyist writing my first app so whereas proper developers would have known this, I didn't have a clue! My app is looking fabulous now though :)
Replies
Boosts
Views
Activity
Apr ’25
SwiftData crashes on fetchHistory
Hi, would it be possible that instead of crashing when calling fetchHistory that function simply throws an error instead? fetchHistory seems to crash when it cannot understand the models if they are not compatible etc… which is understandable, but it makes it really difficult to handle and debug, there's not a lot of details, and honestly I would just rather that it throws an error and let me ignore a history entry that might be useless rather than crashing the entire app. Thank you!
Replies
1
Boosts
0
Views
95
Activity
Apr ’25
Reply to Mac App Crashing with Illegal Instructions
After some digging I found out that SwiftData is causing the problem. More concrete relationships I have a class Account with has (optional) categories @Relationship(deleteRule: .cascade, inverse: Category.account) var _categories: [Category]? var categories: [Category] { self._categories ?? [] } in the class Category I have the a simple account variable var account: Account The application crahses when I use the following init function in Account (at the last line) init(id: Int, income: Bool, location: String, maintainance: Bool, special: Bool, taxRate: Float, tenant: String, title: String) { self.id = id self.income = income self.location = location self.maintainance = maintainance self.special = special self.taxRate = taxRate self.tenant = tenant self.title = title self._categories = [] } or when I use those lines // category not found, so create one let category: Category = Category(title: categoryName, account: self) if self._categories == nil { self._categories = [category] }
Replies
Boosts
Views
Activity
Apr ’25
Why does a Live Activity get .dismissed by the system even when all conditions seem normal?
Hello, I'm working with Live Activities and noticed that sometimes an activity transitions to .dismissed, even though the user hasn't manually swiped it away and system conditions appear to be completely normal — such as: The activity is still within its intended 8-hour lifetime The battery level is high The app is active or recently active The activity is lightweight (not using frequent updates) According to the ActivityKit documentation: /// The Live Activity ended and is no longer visible because a person or the system removed it. case dismissed This doesn’t clarify why the system would dismiss an activity when all conditions seem fine. Additional context: When reviewing system logs via Console.app, we’re seeing messages such as: liveactivitiesd Removing activity from replicator: 381F3DDC-585B-4021-B075-548606F543DA for relationship IDs: [C7AB9C2A-49DD-43FC-BB58-D768ECF9D354] This suggests that the system is actively removing the activity, but there’s no API or reason provided that helps us unders
Replies
1
Boosts
0
Views
235
Activity
Apr ’25
Can DocC be used to abstract out lines of comments?
I'd like to point to comments about my code from outside the coding file to keep the code easier to read, but still have the comments show in Xcode's Quick Help. I've grown to appreciate lengthier comments, including Examples for demonstrating values that explain what calculations are doing. My DocC formatted comments have quickly reach 25 lines and some would benefit from even more lines than that, which I do not want to bloat my code files with. I have tried to find a way to abstract these lines into a .docc file that references the given property, and the following markdown successfully shows this when I run Build Documentation. However, clicking on this property fails to show any comments in Quick Help, even though they did prior to my moving them to an .md file. Unfortunately, I'd prefer reading these in the future in Quick Help over Xcode's Developer Documentation Window. I haven't used markdown in decades, so I'm hoping I'm merely making a markdown mistake someone can find below. For reference, InvItem
Replies
2
Boosts
0
Views
134
Activity
Apr ’25
Reply to SwiftData and iCloud
You might consider starting with the following post: Swiftdata + Cloudkit + Mac OS how to configure for existing Swift Data store Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Apr ’25
SwiftData assertionFailure crash in release builds?
I have an issue in my app, where the crashing frame is an assertionFailure in BackingData.set inside SwiftData framework. My own app doesn't appear until frame 14. I have no idea what causes this, or even how to create a reproducible project as this only happens on some devices. The frame prior to the assertionFailure is this: #1 (null) in BackingData.set(any:value:) () It seems like there is a backing data encoding happening in my Model class, and some value is causing it to fail. The model being accessed is through a relationship, and the frame in the app crashing is along the lines of Text(parent.child.name) Obviously, something is wrong in how I have made child, but the part that stand out to me is the assertionFailure in a release build
Replies
4
Boosts
0
Views
127
Activity
Apr ’25
SwiftData and iCloud
I'm a first time developer for Swift, (getting on a bit!) but after programming in VB back in the late 90s I wanted to write an app for iPhone. I think I might have gone about it the wrong way, but I've got an app that works great on my iPhone or works great on my iPad. It saves the data persistently on device, but, no matter how much I try, what I read and even resorting to AI (ChatGPT & Gemini) I still can't get it to save the data on iCloud to synchronise between the two and work across the devices. I think it must be something pretty fundamental I'm doing (or more likely not doing) that is causing the issue. I'm setting up my signing and capabilities as per the available instructions but I always get a fatal error. I think it might be something to do with making fields optional, but at this point I'm second guessing myself and feeling a complete failure. Any advice or pointers would be really gratefully appreciated. I like my app and would like eventually to get it on the App Store but at this point i
Replies
2
Boosts
0
Views
198
Activity
Apr ’25
Reply to @SectionedFetchRequest using relationship as sectionIdentifier
I was having the same issue and fixed it by adding at least one of the relationship's keys to the sort descriptors of the request. The issue is that the request is not pre-fetching the relationship key-path because it's not included in the request in any capacity). So it seems using it as a section identifier isn't sufficient. I'm assuming SectionedFetchRequest does grouping after the request has returned its results.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’25
Swift Data initiate
I am working with SwiftData and get the below error. I can't find any documentation on it to see what to fix. Any help would be appreciated. Fatal error: This relationship already has a value but it's not the target:
Replies
1
Boosts
0
Views
109
Activity
Apr ’25
Reply to SwiftData Predicates crashes when using Generic
Ran into this as well. The compiler is optimizing something incorrectly for release builds. Using Predicate init directly (instead of the Macro) doesn't resolve the issue. Nor does any combo of explicit/verbose casting. Something as simple as this: func fetchModel(with id: UUID, prefetching relationshipKeyPaths: [PartialKeyPath] = []) throws -> M where M: PersistentModel, M.ID == UUID { var fetchDescriptor = FetchDescriptor() fetchDescriptor.relationshipKeyPathsForPrefetching = relationshipKeyPaths fetchDescriptor.predicate = #Predicate { $0.id == id } fetchDescriptor.fetchLimit = 1 guard let model: M = try? modelContext.fetch(fetchDescriptor).first else { throw SwiftDataRepoError.modelNotFound(withId: id) } return model } Results in this crash: SwiftData/DataUtilities.swift:85: Fatal error: Couldn't find Person. on Person with fields [SwiftData.Schema.PropertyMetadata(name: id, keypath: Person., defaultValue: Optional(5CAE942A-EF4E-4B89-A777-D79153C7F276), metadata: nil) .... Only thing that work
Replies
Boosts
Views
Activity
Apr ’25
Question about BGAppRefreshTask approach for medication scheduling app
I'm developing a medication scheduling app similar to Apple Health's Medications feature, and I'd like some input on my current approach to background tasks. In my app, when a user creates a medication, I generate ScheduledDose objects (with corresponding local notifications) for the next 2 weeks and save them to SwiftData. To ensure this 2-week window stays current, I've implemented a BGAppRefreshTask that runs daily to generate new doses as needed. My concern is whether BGAppRefreshTask is the appropriate mechanism for this purpose. Since I'm not making any network requests but rather generating and storing local data, I'm questioning if this is the right approach. I'm also wondering how Apple Health's Medications feature handles this kind of scheduling. Their app seems to maintain future doses regardless of app usage patterns. Has anyone implemented something similar or can suggest the best background execution API for this type of scenario? Thanks for any guidance you can provide.
Replies
2
Boosts
0
Views
193
Activity
Apr ’25
SwiftData and @Query to find all records for the current date of a multidatepicker (Set = [])
I’m trying to build a CRUD app using SwiftData, @Query model and multidatepicker. The data from a multidatepicker is stored or persists in SwiftData as Set = []. My current dilemma is how to use SwiftData and @Query model Predicate to find all records on the current date. I can’t find any SwiftData documentation or examples @Query using Set = []. My CRUD app should retrieve all records for the current date. Unfortunately, I don’t know the correct @Query model syntax for Set = [].
Replies
0
Boosts
0
Views
76
Activity
Apr ’25
Reply to SwiftData "Auto Inserts" array into ModelContext
SwiftData does it automatically for you and it must do it or the autosave functionality wouldn't work. If you would only save one side of a relationship then when the app is restarted the other side would be nil or it crashes if the property is non-optional.
Replies
Boosts
Views
Activity
Apr ’25