Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

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 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 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
Xcode SwiftUI Preview "app" wants to access data from other app
I am writing SwiftData app, using a group container. When editing a SwiftUI file, every couple of seconds a dialog - that the app wants to access data from other apps - pops up. It is impossible to edit a view file while Canvas preview is open. If preview is resumed the dialog has to be confirmed twice. Each time the app is started from Xcode, the dialog has to be confirmed again. Any idea, how to stop these boring dialogs?
2
0
441
Dec ’24
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
Unable to use transitions for SwiftData in List
I can't for the life of me get transitions and animations to work well with SwiftData and List on MacOS 15 and iOS 18. I've included an example below, where I define several animations and a transition type, but they are all ignored. How do I animate items being added to / removed from a List()? I am attached to List() due to its support for selection, context menu, keyboard shortcuts, etc. If I would switch to ScrollView with VStack I would have to rebuild all of that. Also, this is super basic and should just work, right? Thanks for reading. import SwiftUI import SwiftData struct ContentView: View { @Environment(.modelContext) private var modelContext /// Issues on iOS: /// Items animate into and out of view, but I seem to have no control over the animation. /// In the code here I've specified a 'bouncy' and a slow 'easeIn' animation: both are not triggered. /// The code also specifies using a 'slide' transition, but it is ignored. /// -> How do I control the transition and animation ti
5
0
1.6k
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 relationship crash on 17.x
Is this Relationship correct? Does this cause a circular reference? This runs on 18 but crashes on 17 in the swift data internals. @Model final class Item { @Attribute(.unique) var id: UUID var date: Date @Relationship(deleteRule: .nullify, inverse: Summary.item) var summary: Summary? init(date: Date = Date.now) { self.id = UUID() self.date = Calendar.current.startOfDay(for: date) self.summary = Summary(self) } } @Model final class Summary { @Attribute(.unique) var id = UUID() @Relationship var item: Item? init(_ item: Item) { self.item = item } }
6
0
863
Oct ’24
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
@SectionedFetchRequest using relationship as sectionIdentifier
As the title says I'm trying to use a to-one relationship as a sectionIdentifier in a @SectionedFetchRequest. The compiler is happy but there's a runtime crash: Could not cast value of type '_NSCoreDataTaggedObjectID' (0x146c0f750) to 'MyApp.ServiceCategory' (0x104c4b3a0). The fetch request: @SectionedFetchRequest( sectionIdentifier: Service.serviceCategory, sortDescriptors: [ SortDescriptor(Service.active, order: .reverse), SortDescriptor(Service.displayText) ], predicate: NSPredicate(format: %K = %d, #keyPath(Service.active), true), animation: .default ) var sectionedServices: SectionedFetchResults ... and the breaking runtime code: ForEach(sectionedServices /* here */) { section in Section(header: Text(section.id?.displayText ?? )) { ForEach(section) { svc in Text(svc.displayText ?? ) } } } The request works if I switch out the sectionIdentifier for the active property (which is a Bool property rather than a relationship). It also works if I switch it out for displayText which is an optio
4
0
2.3k
Jul ’21
SwiftData lazy loaded list
Are SwiftData queries lazy loaded when used in conjunction with SwiftUI List? @Query var posts: [PostModel] List { ForEach(posts, id: .id) { post in PostView(post) } } If the code above is not lazy loaded, how can we make it lazy loaded?
Replies
3
Boosts
0
Views
636
Activity
Nov ’24
Reply to Crash in Widget extension
I'm using SwiftData in getTimeline() and I get the same crash
Replies
Boosts
Views
Activity
Apr ’25
Reply to Mac App Crashing with Illegal Instructions
Maybe the underscore in the property name is causing an issue, could you try with a different name? And shouldn't that property be private so you can only access the relationship in one way?
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 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 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
Xcode SwiftUI Preview "app" wants to access data from other app
I am writing SwiftData app, using a group container. When editing a SwiftUI file, every couple of seconds a dialog - that the app wants to access data from other apps - pops up. It is impossible to edit a view file while Canvas preview is open. If preview is resumed the dialog has to be confirmed twice. Each time the app is started from Xcode, the dialog has to be confirmed again. Any idea, how to stop these boring dialogs?
Replies
2
Boosts
0
Views
441
Activity
Dec ’24
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
Unable to use transitions for SwiftData in List
I can't for the life of me get transitions and animations to work well with SwiftData and List on MacOS 15 and iOS 18. I've included an example below, where I define several animations and a transition type, but they are all ignored. How do I animate items being added to / removed from a List()? I am attached to List() due to its support for selection, context menu, keyboard shortcuts, etc. If I would switch to ScrollView with VStack I would have to rebuild all of that. Also, this is super basic and should just work, right? Thanks for reading. import SwiftUI import SwiftData struct ContentView: View { @Environment(.modelContext) private var modelContext /// Issues on iOS: /// Items animate into and out of view, but I seem to have no control over the animation. /// In the code here I've specified a 'bouncy' and a slow 'easeIn' animation: both are not triggered. /// The code also specifies using a 'slide' transition, but it is ignored. /// -> How do I control the transition and animation ti
Replies
5
Boosts
0
Views
1.6k
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
SwiftData relationship crash on 17.x
Is this Relationship correct? Does this cause a circular reference? This runs on 18 but crashes on 17 in the swift data internals. @Model final class Item { @Attribute(.unique) var id: UUID var date: Date @Relationship(deleteRule: .nullify, inverse: Summary.item) var summary: Summary? init(date: Date = Date.now) { self.id = UUID() self.date = Calendar.current.startOfDay(for: date) self.summary = Summary(self) } } @Model final class Summary { @Attribute(.unique) var id = UUID() @Relationship var item: Item? init(_ item: Item) { self.item = item } }
Replies
6
Boosts
0
Views
863
Activity
Oct ’24
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
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
@SectionedFetchRequest using relationship as sectionIdentifier
As the title says I'm trying to use a to-one relationship as a sectionIdentifier in a @SectionedFetchRequest. The compiler is happy but there's a runtime crash: Could not cast value of type '_NSCoreDataTaggedObjectID' (0x146c0f750) to 'MyApp.ServiceCategory' (0x104c4b3a0). The fetch request: @SectionedFetchRequest( sectionIdentifier: Service.serviceCategory, sortDescriptors: [ SortDescriptor(Service.active, order: .reverse), SortDescriptor(Service.displayText) ], predicate: NSPredicate(format: %K = %d, #keyPath(Service.active), true), animation: .default ) var sectionedServices: SectionedFetchResults ... and the breaking runtime code: ForEach(sectionedServices /* here */) { section in Section(header: Text(section.id?.displayText ?? )) { ForEach(section) { svc in Text(svc.displayText ?? ) } } } The request works if I switch out the sectionIdentifier for the active property (which is a Bool property rather than a relationship). It also works if I switch it out for displayText which is an optio
Replies
4
Boosts
0
Views
2.3k
Activity
Jul ’21