Search results for

“SwiftData inheritance relationship”

4,980 results found

Post

Replies

Boosts

Views

Activity

Reply to Finding source for SwiftData array behaviour
Yeah, that is what I mentioned above, and it's because a too-many relationship is expressed as a set (NSSet) in Core Data, which is used by SwiftData default store. I don't think the behavior is formally documented. If you don't mind, please file a feedback report. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’25
SwiftData UndoManager leaks?
In any SwiftData project with undo enabled, I noticed that repeatedly undoing and redoing a change will cause memory to continuously climb. The larger the change, the more memory is consumed. This is surprising to me. I confirmed this on iOS and macOS using Xcode 15.4 and 16 beta. To reproduce, simply add undo/redo functionality to any project and repeatedly undo/redo a change. Memory consumption will climb continuously and will not get released. In Paul Hudson's SwiftData tutorials, he has an entire section about the numerous tricks required to get SwiftData relationships to work correctly. Does anyone know if SwiftData also requires tricks to get the UndoManager to work correctly?
1
0
428
Jun ’24
Reply to SwiftData and 'Circular references'
Ok, after reviewing the 'Trips' sample application that accompanied the 'Dive deeper into SwiftData' video from WWDC '23, I see that this is handled between the Trip and both the BucketListItem and LivingAccommodation entities by simply omitting the @Relationship annotation on the 'child' entities. I have a few questions, based on what I see: If a LivingAccommodation entity is loaded via a @Query, will the trips field be populated by the SwiftData system? On both child entities, the trip field is an Optional Trip - is this required in this scenario? I would imagine that it doesn't make sense to have a LivingAccommodation object without an associated Trip object, and my own Model is similar
Jun ’23
Reply to Cannot invoke 'dataTask' with an argument list of type...
That would make very much Android programming... frankly I do not see so much the need to create so many layers in an object oriented environment but rather have in my full view the full piece of code I am working with. But of course this is a matter of taste. Somone like it horizontal, some othe ones like it vertical :-) It mins me of the bias, also being enforced in the WWDC, towards the have relationoship, oppsotie to the is one (property verus inheritance). OO theory has weel gaounded criteria to decide the relationships bwtween classes o to better model reality and I hold any attempt to steer somewhere for this make for not so easily maintanable code. But again this is a matter of taste.
Topic: Programming Languages SubTopic: Swift Tags:
Sep ’16
Using relationships in SortDescriptor crashing on release
If use a SortDescriptor for a model and sort by some attribute from a relationship, in DEBUG mode it all works fine and sorts. However, in release mode, it is an instant crash. SortDescriptor(.name, order: .reverse) ---- works SortDescriptor(.assignedUser?.name, order: .reverse) ---- works in debug but crash in release. What is the issue here, is it that SwiftData just incompetent to do this?
2
0
125
Mar ’25
Does @Relationship(inverse:) create a memory leak?
Hi, I am creating (or trying to) my first app using SwiftData - and I have questions :-) The main question I can't get my head wrapped around is the following: Let's say I have the sample below... @Model class Person { @Relationship(inverse:Hat.owner) var hat:Hat } @Model class Hat { var owner:Person? } It looks like I am creating a strong reference cycle between the person and the hat objects? And in fact I am seeing these kinds of reference cycles when I look at the memory debugger. Many code samples I have seen so far use this type of relationship declaration... And I am wondering: Am I missing something? Admittedly I don't find many discussions about memory leaks caused by SwiftData despite the syntax being used in many examples? So what is the situation? Did Apple just miss to explain that the inverse: declaration causes memory leaks or is there some kind of magic that I should understand?
2
0
163
Mar ’25
Reply to SwiftData self-referencing model has wierd behavior
I have been trying to do something similar. I think you may need an inverse to the children collection. This is what I have done and it seems to work. If someone else knows better I'd be delighted to be schooled otherwise :-) import Foundation import SwiftData @Model final class TreeNodeModel{ var createdAt: Date var title: String var content: String // Parental relationship public var parent: TreeNodeModel? // Inverse @Relationship(deleteRule:.cascade, inverse: TreeNodeModel.parent) var children: [TreeNodeModel]? init(title: String, content: String = ) { self.title = title self.content = content } }
Aug ’23
Navigation Bar colour in PHPhotoPicker does not inherit Application's navigation bar colour in iOS 16
Recently I updated my device OS to the iOS16 version and observed one discrepancy in the native PHPhotoPicker Component in my application. In the Previous version (i.e iOS 15.3), PHPhotoPicker inherits the application's navigation bar color (screenshot attached for reference) However, In the iOS 16 version, PHPhotoPicker's navigation bar color does not change according to the application theme, And PHPhotoPicker displays with its default UI (screenshot attached for it)
0
0
817
Oct ’22
How to implement relationship
Hi,I'm new in Core data.What I want to achive is have to entities - Documents and Sales.Sales should have: price, productname, qtyDocuments should have: date, documentkind, number etc.What I want is create a relationship between them, because every document will have one or more sales, these are going to be the product items.I know how to achive this when I bind one to one, but when Documents should have more than one Sales....?Is this the right aproach?
3
0
477
Jun ’17
Relationships are not persisted unless there is an inverse?
Hi, I encountered the issue, that unless an inverse relationship is modelled, the relationship is not persisted. This can be reproduced with the sample code below: Press the Add Person button twice Then press the Add group button You now can see that the group has to member, but once you restart the app the members a gone. Once an inverse relationship is added (see commented code) the relationships are persisted. Any idea if this is intended behaviour? import SwiftData import SwiftUI // MARK: - Person - @Model class Person { var name: String // uncomment to make it work @Relationship(.nullify) var group: Group? init(name: String) { self.name = name } } // MARK: - Group - @Model class Group { var name: String // uncomment to make it work @Relationship(.nullify, inverse: Person.group) public var members: [Person] @Relationship(.nullify) public var members: [Person] // comment to make it work init(name: String) { self.name = name } } // MARK
0
0
1.4k
Jun ’23
Reply to Does @Relationship(inverse:) create a memory leak?
No, there is no reference cycle and memory leak here. By using @Model, hat and owner are converted to computed properties, and some logics are implemented there to maintain the integrity of the relationship. Those details are barely documented, but conceptually, SwiftData takes care the integrity and so you don't need to worry about that. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Mar ’25
Label color is not inheriting from body
I have a site with forms that uses jQuery UI ThemeRoller (http://jqueryui.com/themeroller/) for theming, and on Safari Preview and iOS 10, the labels have suddenly turned white.The work on all other browsers (as they have for years).When I inspect, it appears that the body color is being overridden by the ui-composition-content class, when previously it was not.My workaround is to add a specific color tag for the labels, to reset them to body (which is what they should be):label { color: #000000; }Any ideas on why the new Safari is no longer rendering as expected ?
0
0
449
Oct ’16
Reply to protocol extensions - default implementations vs. helper methods
It gets a little clearer if you understand that the relationship between methods in the protocol extension and methods in the conforming type is NOT inheritance. It's something new — call it replacement or something. This seems bizarre until you realize that protocol extensions are available for struct types as well as class types, and struct types do not have subclassing/inheritance. If conforming types were able to override protocol extension methods, then the subclassing mechanism would be effectively extended to struct types, which presumably is never going to happen.The difference of behavior between extension methods that appear as protocol requirements and ones that don't is explicitly documented, so you may not like it but it is what it is.However there is one horrible bug in the current implementation (bug #21470728). If you have a subclass of a class that adopts the protocol but the class does not provide its own version of a not-required method defined in the protocol ext
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’15
CoreData to-Many relationship saving NSSet relationship property
I have an ShoppingItem Entity(attributes: name, id, unit, qty, description) and another entity ShoppingList(attributes: listDate, listName) and a relationship property : items which is many-to-many relationship. The use case is : _The items that are selected by the user have to be updated to the list on the particular date. let myList = GroceryList(context: CoreDataManager.shared.viewContext) myList.name = One more List myList.madeOn = Date() 1. myList.mutableSetValue(forKey: groceryItems).add(selectedGroceryItems) CoreDataManager.shared.save() selectedGroceryItems is of type struct ItemEntityViewModel { let groceryItem: GroceryItem var itemEntityid: NSManagedObjectID { return groceryItem.objectID } var id: UUID { return groceryItem.id ?? UUID() } var name: String { return groceryItem.name ?? } var category: String { return groceryItem.category ?? Not available } var unit: String? { return groceryItem.unit ?? } var qty: Double? { return Double(groceryItem.qty) } } line 1 throws exception a
3
0
570
Oct ’21
Reply to Finding source for SwiftData array behaviour
Yeah, that is what I mentioned above, and it's because a too-many relationship is expressed as a set (NSSet) in Core Data, which is used by SwiftData default store. I don't think the behavior is formally documented. If you don't mind, please file a feedback report. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’25
SwiftData UndoManager leaks?
In any SwiftData project with undo enabled, I noticed that repeatedly undoing and redoing a change will cause memory to continuously climb. The larger the change, the more memory is consumed. This is surprising to me. I confirmed this on iOS and macOS using Xcode 15.4 and 16 beta. To reproduce, simply add undo/redo functionality to any project and repeatedly undo/redo a change. Memory consumption will climb continuously and will not get released. In Paul Hudson's SwiftData tutorials, he has an entire section about the numerous tricks required to get SwiftData relationships to work correctly. Does anyone know if SwiftData also requires tricks to get the UndoManager to work correctly?
Replies
1
Boosts
0
Views
428
Activity
Jun ’24
Reply to SwiftData and 'Circular references'
Ok, after reviewing the 'Trips' sample application that accompanied the 'Dive deeper into SwiftData' video from WWDC '23, I see that this is handled between the Trip and both the BucketListItem and LivingAccommodation entities by simply omitting the @Relationship annotation on the 'child' entities. I have a few questions, based on what I see: If a LivingAccommodation entity is loaded via a @Query, will the trips field be populated by the SwiftData system? On both child entities, the trip field is an Optional Trip - is this required in this scenario? I would imagine that it doesn't make sense to have a LivingAccommodation object without an associated Trip object, and my own Model is similar
Replies
Boosts
Views
Activity
Jun ’23
Reply to Cannot invoke 'dataTask' with an argument list of type...
That would make very much Android programming... frankly I do not see so much the need to create so many layers in an object oriented environment but rather have in my full view the full piece of code I am working with. But of course this is a matter of taste. Somone like it horizontal, some othe ones like it vertical :-) It mins me of the bias, also being enforced in the WWDC, towards the have relationoship, oppsotie to the is one (property verus inheritance). OO theory has weel gaounded criteria to decide the relationships bwtween classes o to better model reality and I hold any attempt to steer somewhere for this make for not so easily maintanable code. But again this is a matter of taste.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’16
Using relationships in SortDescriptor crashing on release
If use a SortDescriptor for a model and sort by some attribute from a relationship, in DEBUG mode it all works fine and sorts. However, in release mode, it is an instant crash. SortDescriptor(.name, order: .reverse) ---- works SortDescriptor(.assignedUser?.name, order: .reverse) ---- works in debug but crash in release. What is the issue here, is it that SwiftData just incompetent to do this?
Replies
2
Boosts
0
Views
125
Activity
Mar ’25
Does @Relationship(inverse:) create a memory leak?
Hi, I am creating (or trying to) my first app using SwiftData - and I have questions :-) The main question I can't get my head wrapped around is the following: Let's say I have the sample below... @Model class Person { @Relationship(inverse:Hat.owner) var hat:Hat } @Model class Hat { var owner:Person? } It looks like I am creating a strong reference cycle between the person and the hat objects? And in fact I am seeing these kinds of reference cycles when I look at the memory debugger. Many code samples I have seen so far use this type of relationship declaration... And I am wondering: Am I missing something? Admittedly I don't find many discussions about memory leaks caused by SwiftData despite the syntax being used in many examples? So what is the situation? Did Apple just miss to explain that the inverse: declaration causes memory leaks or is there some kind of magic that I should understand?
Replies
2
Boosts
0
Views
163
Activity
Mar ’25
Reply to SwiftData self-referencing model has wierd behavior
I have been trying to do something similar. I think you may need an inverse to the children collection. This is what I have done and it seems to work. If someone else knows better I'd be delighted to be schooled otherwise :-) import Foundation import SwiftData @Model final class TreeNodeModel{ var createdAt: Date var title: String var content: String // Parental relationship public var parent: TreeNodeModel? // Inverse @Relationship(deleteRule:.cascade, inverse: TreeNodeModel.parent) var children: [TreeNodeModel]? init(title: String, content: String = ) { self.title = title self.content = content } }
Replies
Boosts
Views
Activity
Aug ’23
Navigation Bar colour in PHPhotoPicker does not inherit Application's navigation bar colour in iOS 16
Recently I updated my device OS to the iOS16 version and observed one discrepancy in the native PHPhotoPicker Component in my application. In the Previous version (i.e iOS 15.3), PHPhotoPicker inherits the application's navigation bar color (screenshot attached for reference) However, In the iOS 16 version, PHPhotoPicker's navigation bar color does not change according to the application theme, And PHPhotoPicker displays with its default UI (screenshot attached for it)
Replies
0
Boosts
0
Views
817
Activity
Oct ’22
How transient relationship works in CoreData
When open a CoreData relationship in the Data Model inspector, it shows an option to set the relationship as transient. Apple Core Data Programming Guide havent explained how transient relationship works. Can you please explain, how transient relationship works?
Replies
1
Boosts
0
Views
1.7k
Activity
Aug ’16
How to implement relationship
Hi,I'm new in Core data.What I want to achive is have to entities - Documents and Sales.Sales should have: price, productname, qtyDocuments should have: date, documentkind, number etc.What I want is create a relationship between them, because every document will have one or more sales, these are going to be the product items.I know how to achive this when I bind one to one, but when Documents should have more than one Sales....?Is this the right aproach?
Replies
3
Boosts
0
Views
477
Activity
Jun ’17
Relationships are not persisted unless there is an inverse?
Hi, I encountered the issue, that unless an inverse relationship is modelled, the relationship is not persisted. This can be reproduced with the sample code below: Press the Add Person button twice Then press the Add group button You now can see that the group has to member, but once you restart the app the members a gone. Once an inverse relationship is added (see commented code) the relationships are persisted. Any idea if this is intended behaviour? import SwiftData import SwiftUI // MARK: - Person - @Model class Person { var name: String // uncomment to make it work @Relationship(.nullify) var group: Group? init(name: String) { self.name = name } } // MARK: - Group - @Model class Group { var name: String // uncomment to make it work @Relationship(.nullify, inverse: Person.group) public var members: [Person] @Relationship(.nullify) public var members: [Person] // comment to make it work init(name: String) { self.name = name } } // MARK
Replies
0
Boosts
0
Views
1.4k
Activity
Jun ’23
Reply to Does @Relationship(inverse:) create a memory leak?
No, there is no reference cycle and memory leak here. By using @Model, hat and owner are converted to computed properties, and some logics are implemented there to maintain the integrity of the relationship. Those details are barely documented, but conceptually, SwiftData takes care the integrity and so you don't need to worry about that. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Mar ’25
Label color is not inheriting from body
I have a site with forms that uses jQuery UI ThemeRoller (http://jqueryui.com/themeroller/) for theming, and on Safari Preview and iOS 10, the labels have suddenly turned white.The work on all other browsers (as they have for years).When I inspect, it appears that the body color is being overridden by the ui-composition-content class, when previously it was not.My workaround is to add a specific color tag for the labels, to reset them to body (which is what they should be):label { color: #000000; }Any ideas on why the new Safari is no longer rendering as expected ?
Replies
0
Boosts
0
Views
449
Activity
Oct ’16
Reply to protocol extensions - default implementations vs. helper methods
It gets a little clearer if you understand that the relationship between methods in the protocol extension and methods in the conforming type is NOT inheritance. It's something new — call it replacement or something. This seems bizarre until you realize that protocol extensions are available for struct types as well as class types, and struct types do not have subclassing/inheritance. If conforming types were able to override protocol extension methods, then the subclassing mechanism would be effectively extended to struct types, which presumably is never going to happen.The difference of behavior between extension methods that appear as protocol requirements and ones that don't is explicitly documented, so you may not like it but it is what it is.However there is one horrible bug in the current implementation (bug #21470728). If you have a subclass of a class that adopts the protocol but the class does not provide its own version of a not-required method defined in the protocol ext
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’15
CoreData to-Many relationship saving NSSet relationship property
I have an ShoppingItem Entity(attributes: name, id, unit, qty, description) and another entity ShoppingList(attributes: listDate, listName) and a relationship property : items which is many-to-many relationship. The use case is : _The items that are selected by the user have to be updated to the list on the particular date. let myList = GroceryList(context: CoreDataManager.shared.viewContext) myList.name = One more List myList.madeOn = Date() 1. myList.mutableSetValue(forKey: groceryItems).add(selectedGroceryItems) CoreDataManager.shared.save() selectedGroceryItems is of type struct ItemEntityViewModel { let groceryItem: GroceryItem var itemEntityid: NSManagedObjectID { return groceryItem.objectID } var id: UUID { return groceryItem.id ?? UUID() } var name: String { return groceryItem.name ?? } var category: String { return groceryItem.category ?? Not available } var unit: String? { return groceryItem.unit ?? } var qty: Double? { return Double(groceryItem.qty) } } line 1 throws exception a
Replies
3
Boosts
0
Views
570
Activity
Oct ’21