Search results for

“SwiftData inheritance relationship”

4,980 results found

Post

Replies

Boosts

Views

Activity

How do I resolve conflicts with SwiftData?
SwiftData includes support for CloudKit sync. However, I don't see any way to add conflict resolution behavior. For example, if different devices set different values for a field, or if a relationship is orphaned because of a deletion on another device, the application has to handle this somehow. In Core Data (which SwiftData wraps), you can handle this with the conflict resolution system (docs) and classes like NSMergePolicy. Is any of this accessible in SwiftData? If not, how do you deal with conflicts when syncing a SwiftData model with the cloud?
2
0
1.4k
May ’24
Is there a way for Embed in -> to inherit / copy over existing constraints?
Is there a way for Embed in -> to inherit / copy over existing constraints?Basically, say I have a UIView, and I set it to 20 from top / leading / trailing / bottom.Then later, I decide I want to embed that in another UIView. Using the embed in menu it loses all of its constraints. Is there a easy way to keep the constraints and it just remaps them from Superview to my new UIView? I know this is a simple example but think a UIView with labels, text, etc, etc all positioned to the safespace, and when I select them and embed in a UIView it essentially destroys all the previous work and makes me start over reconnecting all my constraints. I can see how sometimes it wouldn't make sense to keep the constraints, but it really would be nice to reapply as many relevant constraints as possible. Is there something I am missing that would make this type of process easier? even 3rd party or some other method I am overlooking? Thanks!
3
0
991
Apr ’19
Non-designated initialiser inheritance from Objective C classes
Having come across problems when sub-classing UIKit classes and adding immutable variables to them, I made a test project to figure out what was going on.My conclusion is that ifwe have an Objective C class, which inherits from another class, with its own designated initialiser (implicit or explicitly annotated)in its initialiser, it calls [self initWithXX] where initWithXX is an init method on the superclasswe subclass this class in Swift, adding an immutable property (which obviously must be initialised on instantiation)we implement a single designated initialiser for this Swift class which sets the immutable property then calls the parent class designated initialiserthen this will cause a runtime exception because the Swift class, when calling the Objective C superclass's designated initialiser, will attempt to call initWithXX on self and this method has not been inherited from the superclass because we have implemented a designated initialiser.The code for this test would be:View.h#impor
4
0
1.8k
Jul ’15
Predicate based on the relationship
I am trying to use SwiftData to perform a Query based on the name of the actor, which is inside a movie model. Here is the Movie model. @Model final class Movie { var title: String var year: Int @Relationship(.noAction, inverse: Actor.movies) var actors: [Actor] = [] init(title: String, year: Int) { self.title = title self.year = year } } Here is my actual Query: _movies = Query(filter: #Predicate { $0.actors.contains(where: { $0.name.contains(actorName) }) }) But it returns nothing, even though I am passing actorName which exists in the movie.
3
0
1.9k
Jun ’23
Fetching data with relationships directly faults the relationships even when not accessed
I am using SwiftData to model my data. For that i created a model called OrganizationData that contains various relationships to other entities. My data set is quite large and i am having a big performance issue when fetching all OrganizationData entities. I started debugging and looking at the sql debug log i noticed that when fetching my entities i run into faults for all relationships even when not accessing them. Fetching my entities: let fetchDescriptor = FetchDescriptor() let context = MapperContext(dataManager: self) let organizations = (try modelContainer.mainContext.fetch(fetchDescriptor)) Doing this fetch, also fetches all relationships. Each in a single query, for every OrganizationData entity. CoreData: annotation: to-many relationship fault relationship1 for objectID 0x8aa5249772916e00 fulfilled from database. Got 9 rows CoreData: annotation: to-many relationship fault relationship2 for objectID 0x8aa5249772916e00 fulfilled from database. Go
14
0
454
May ’25
Required inits + convenience inits + inheritance == conundrum
While trying to convert an older Objective-C program to Swift, I've run into a hitch.So, let's say we've got a base class with a required initializer:class Base { required init(foo: Foo) { … } }and then we have a subclass, but the subclass includes a new parameter in the init, specific to the subclass, and satisfies the requirement via a convenience initializer:class Subclass: Base { required init(foo: Foo, bar: Bar) { ... super.init(foo: foo) ... } required convenience init(foo: Foo) { // Do some calculation to determine a default value for ‘bar’ from the input of ‘foo’. // Perhaps this calculation is quite a few lines of code. let bar = ... self.init(foo: foo, bar: bar) } }Now, that subclass gets subclassed once again:class SubSubclass: Subclass { required init(foo: Foo, bar: Bar) { … super.init(foo: Foo, bar: Bar) ... } required convenience init(foo: Foo) { // Uh oh. } }The trouble here is that we’re required to implement init(foo:) per the base class’s interface, but we cannot use Subclass’s perfectly ser
2
0
2k
Dec ’15
Inherit GKGoal or GKBehavior
Hello there,I've been playing with Agents, Goals & Behaviors ecosystem and I must admit it's great so far (really time saving).I'd like to add a behavior to some of my entities: make them go in one direction, and once an obstacle has been hit, bounce in the other direction (like in the old 80's games). I know how to do this the classical way - using physics or not - but I was wondering if trying to implement it using the Agents and/or Pathfinding system is a good or bad choice here? Any advice?Thanks!
1
0
383
Aug ’15
Reply to SwiftData does not cascade delete
Also not working for me...child objects are not being deleted when using the .cascade option. Could someone be abe to assist? I'm using SwiftData with CloudKit enabled. Here's a simplified version of my model, where Score objects are not being deleted upon deletion of Match objects : import SwiftData @Model class Match { // CloudKit integration requires all relationship properties to be optional @Relationship(deleteRule: .cascade, inverse:Score.match) var score: Score? init() { self.score = Score( startServing: true, decidingSetType: .regular ) } } @Model class Score { // CloudKit integration requires all relationship properties to be optional var match: Match? // CloudKit integration requires all non optional properties to have a default value let startServing: Bool = true let decidingSetType: DecidingSetType = DecidingSetType.regular init( startServing: Bool, decidingSetType: DecidingSetType ) { self.startServing = startServing self.decidingSetType = decidingSetT
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Use of Inherited Protocol as Typealias
In the following code example, I get a does not conform to protocol on MyStruct.protocol OutsideProtocol { typealias T: InsideProtocol } protocol InsideProtocol { } protocol InheritingProtocol: InsideProtocol { } struct MyStruct: OutsideProtocol { typealias T = InheritingProtocol }This would compile if InheritingProtocol were a struct or class. Is this a known limitation and is there a workaround?
1
0
433
Jul ’15
inheriting var in other class ?
My brain is fried from heat, not even sure how to pose the question.I have a class that has a var let's say var isRunning: Bool = false, within it's parent another class instance with the same var where I want it to be controlled from. If I link the two, initially it's reading the var but changing later has no affect.Sorry, I know this is probably a basic question.
9
0
966
Jun ’19
tvOS inherit system settings
I'm a newby at tvOS and want to know, if it is possible to override some system settings. Especially I want to override the output to audio devices. For the moment (using Apple TV 4K and tvOS 17) you can only select one device (TV, eARC, airPods) and I need a possibility to set a simultaneous output to airPods and TV or eARC. Is such a programming possible?
0
0
500
Jul ’24
TextKit2 : - The text inserted between the attributedText(Paragraph) doesn't inherit the attributes of existing text
I have added an custom attribute for a paragraph using the below method textStorage.addAttribute(.customCase, value: checkList, range: paragraphRange) When I insert some text in between the text which contains the custom attribute, that text is not inheriting/propagating the custom attribute of existing paragraph text Old Text : - This is a test New Text : - This is some new a test The inserted part is not getting the custom attribute of the old text, Can I know why it's happening, Is it some textKit2's behaviour.
0
0
452
Dec ’24
Changes in Swift 5 to inheritance?
We have a strange situation where behavior has changed from Swift 4 -> 5 and I can't figure out what is going wrong. We have our own open base table view controller class in a framework that conforms to UITableViewDatasource and UITableViewDelegate. In Swift 4 the subclasses could simply implement any of those delegate methods and they would work as expected. With Swift 5 and the latest Xcode this has all changed and any signature from those protocols that isn't explicitly written out in the base class are completely ignored, causing all kinds of strange bugs.Our base class implementation looks something like this:open class OsuTableViewController: OsuViewController, UITableViewDelegate, UITableViewDataSource { public let tableView = UITableView(frame: .zero, style: .grouped) public var tableSections = [OsuTableSection]() override open func viewDidLoad() { super.viewDidLoad() tableView.datasource = self tableView.delegate = self } open func numberOfSections(in tableView: UITableView) -> Int { return tab
3
0
4.2k
Apr ’19
Sort on the @relationship property wrapper
Hello Developer Support, Let's say I have the following models. Is there a way to have dataSet pre-sorted by swiftData? @Model Struct Capture { let someInfo: String @Relationship(.unique, deleteRule: .cascade) // some way of sorting by CoolPoint.date let dataSet: [CoolPoint] } @Model struct CoolPoint { let point: Int let date: Date }
0
0
746
Jan ’24
Reply to How to handle required @relationship optionals in SwiftData CloudKit?
SwiftData + CloudKit uses NSPersistentCloudKitContainer under the hood, which requires all relationships must be optional. For more information, see Creating a Core Data Model for CloudKit. The requirement exists because of the latency of the synchronization: When you create an object graph in device A, which is being synchronized to device B, the system doesn't guarantee to synchronize the whole graph all at once. As a result, it's possible that an object is synchronized but its relationship is not. This situation is expressed as the relationship being nil. By checking if the relationship is nil, the app instance running on device B can consume the object appropriately. In your case, wrapping a relationship with a computed property to return an empty array if nil makes sense to me, if the other part of your app prefers to consume an empty array. It doesn't matter if the data is big or small. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Oct ’25
How do I resolve conflicts with SwiftData?
SwiftData includes support for CloudKit sync. However, I don't see any way to add conflict resolution behavior. For example, if different devices set different values for a field, or if a relationship is orphaned because of a deletion on another device, the application has to handle this somehow. In Core Data (which SwiftData wraps), you can handle this with the conflict resolution system (docs) and classes like NSMergePolicy. Is any of this accessible in SwiftData? If not, how do you deal with conflicts when syncing a SwiftData model with the cloud?
Replies
2
Boosts
0
Views
1.4k
Activity
May ’24
Is there a way for Embed in -> to inherit / copy over existing constraints?
Is there a way for Embed in -> to inherit / copy over existing constraints?Basically, say I have a UIView, and I set it to 20 from top / leading / trailing / bottom.Then later, I decide I want to embed that in another UIView. Using the embed in menu it loses all of its constraints. Is there a easy way to keep the constraints and it just remaps them from Superview to my new UIView? I know this is a simple example but think a UIView with labels, text, etc, etc all positioned to the safespace, and when I select them and embed in a UIView it essentially destroys all the previous work and makes me start over reconnecting all my constraints. I can see how sometimes it wouldn't make sense to keep the constraints, but it really would be nice to reapply as many relevant constraints as possible. Is there something I am missing that would make this type of process easier? even 3rd party or some other method I am overlooking? Thanks!
Replies
3
Boosts
0
Views
991
Activity
Apr ’19
Non-designated initialiser inheritance from Objective C classes
Having come across problems when sub-classing UIKit classes and adding immutable variables to them, I made a test project to figure out what was going on.My conclusion is that ifwe have an Objective C class, which inherits from another class, with its own designated initialiser (implicit or explicitly annotated)in its initialiser, it calls [self initWithXX] where initWithXX is an init method on the superclasswe subclass this class in Swift, adding an immutable property (which obviously must be initialised on instantiation)we implement a single designated initialiser for this Swift class which sets the immutable property then calls the parent class designated initialiserthen this will cause a runtime exception because the Swift class, when calling the Objective C superclass's designated initialiser, will attempt to call initWithXX on self and this method has not been inherited from the superclass because we have implemented a designated initialiser.The code for this test would be:View.h#impor
Replies
4
Boosts
0
Views
1.8k
Activity
Jul ’15
Predicate based on the relationship
I am trying to use SwiftData to perform a Query based on the name of the actor, which is inside a movie model. Here is the Movie model. @Model final class Movie { var title: String var year: Int @Relationship(.noAction, inverse: Actor.movies) var actors: [Actor] = [] init(title: String, year: Int) { self.title = title self.year = year } } Here is my actual Query: _movies = Query(filter: #Predicate { $0.actors.contains(where: { $0.name.contains(actorName) }) }) But it returns nothing, even though I am passing actorName which exists in the movie.
Replies
3
Boosts
0
Views
1.9k
Activity
Jun ’23
Fetching data with relationships directly faults the relationships even when not accessed
I am using SwiftData to model my data. For that i created a model called OrganizationData that contains various relationships to other entities. My data set is quite large and i am having a big performance issue when fetching all OrganizationData entities. I started debugging and looking at the sql debug log i noticed that when fetching my entities i run into faults for all relationships even when not accessing them. Fetching my entities: let fetchDescriptor = FetchDescriptor() let context = MapperContext(dataManager: self) let organizations = (try modelContainer.mainContext.fetch(fetchDescriptor)) Doing this fetch, also fetches all relationships. Each in a single query, for every OrganizationData entity. CoreData: annotation: to-many relationship fault relationship1 for objectID 0x8aa5249772916e00 fulfilled from database. Got 9 rows CoreData: annotation: to-many relationship fault relationship2 for objectID 0x8aa5249772916e00 fulfilled from database. Go
Replies
14
Boosts
0
Views
454
Activity
May ’25
Required inits + convenience inits + inheritance == conundrum
While trying to convert an older Objective-C program to Swift, I've run into a hitch.So, let's say we've got a base class with a required initializer:class Base { required init(foo: Foo) { … } }and then we have a subclass, but the subclass includes a new parameter in the init, specific to the subclass, and satisfies the requirement via a convenience initializer:class Subclass: Base { required init(foo: Foo, bar: Bar) { ... super.init(foo: foo) ... } required convenience init(foo: Foo) { // Do some calculation to determine a default value for ‘bar’ from the input of ‘foo’. // Perhaps this calculation is quite a few lines of code. let bar = ... self.init(foo: foo, bar: bar) } }Now, that subclass gets subclassed once again:class SubSubclass: Subclass { required init(foo: Foo, bar: Bar) { … super.init(foo: Foo, bar: Bar) ... } required convenience init(foo: Foo) { // Uh oh. } }The trouble here is that we’re required to implement init(foo:) per the base class’s interface, but we cannot use Subclass’s perfectly ser
Replies
2
Boosts
0
Views
2k
Activity
Dec ’15
Inherit GKGoal or GKBehavior
Hello there,I've been playing with Agents, Goals & Behaviors ecosystem and I must admit it's great so far (really time saving).I'd like to add a behavior to some of my entities: make them go in one direction, and once an obstacle has been hit, bounce in the other direction (like in the old 80's games). I know how to do this the classical way - using physics or not - but I was wondering if trying to implement it using the Agents and/or Pathfinding system is a good or bad choice here? Any advice?Thanks!
Replies
1
Boosts
0
Views
383
Activity
Aug ’15
Reply to SwiftData does not cascade delete
Also not working for me...child objects are not being deleted when using the .cascade option. Could someone be abe to assist? I'm using SwiftData with CloudKit enabled. Here's a simplified version of my model, where Score objects are not being deleted upon deletion of Match objects : import SwiftData @Model class Match { // CloudKit integration requires all relationship properties to be optional @Relationship(deleteRule: .cascade, inverse:Score.match) var score: Score? init() { self.score = Score( startServing: true, decidingSetType: .regular ) } } @Model class Score { // CloudKit integration requires all relationship properties to be optional var match: Match? // CloudKit integration requires all non optional properties to have a default value let startServing: Bool = true let decidingSetType: DecidingSetType = DecidingSetType.regular init( startServing: Bool, decidingSetType: DecidingSetType ) { self.startServing = startServing self.decidingSetType = decidingSetT
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Dec ’23
Use of Inherited Protocol as Typealias
In the following code example, I get a does not conform to protocol on MyStruct.protocol OutsideProtocol { typealias T: InsideProtocol } protocol InsideProtocol { } protocol InheritingProtocol: InsideProtocol { } struct MyStruct: OutsideProtocol { typealias T = InheritingProtocol }This would compile if InheritingProtocol were a struct or class. Is this a known limitation and is there a workaround?
Replies
1
Boosts
0
Views
433
Activity
Jul ’15
inheriting var in other class ?
My brain is fried from heat, not even sure how to pose the question.I have a class that has a var let's say var isRunning: Bool = false, within it's parent another class instance with the same var where I want it to be controlled from. If I link the two, initially it's reading the var but changing later has no affect.Sorry, I know this is probably a basic question.
Replies
9
Boosts
0
Views
966
Activity
Jun ’19
tvOS inherit system settings
I'm a newby at tvOS and want to know, if it is possible to override some system settings. Especially I want to override the output to audio devices. For the moment (using Apple TV 4K and tvOS 17) you can only select one device (TV, eARC, airPods) and I need a possibility to set a simultaneous output to airPods and TV or eARC. Is such a programming possible?
Replies
0
Boosts
0
Views
500
Activity
Jul ’24
TextKit2 : - The text inserted between the attributedText(Paragraph) doesn't inherit the attributes of existing text
I have added an custom attribute for a paragraph using the below method textStorage.addAttribute(.customCase, value: checkList, range: paragraphRange) When I insert some text in between the text which contains the custom attribute, that text is not inheriting/propagating the custom attribute of existing paragraph text Old Text : - This is a test New Text : - This is some new a test The inserted part is not getting the custom attribute of the old text, Can I know why it's happening, Is it some textKit2's behaviour.
Replies
0
Boosts
0
Views
452
Activity
Dec ’24
Changes in Swift 5 to inheritance?
We have a strange situation where behavior has changed from Swift 4 -> 5 and I can't figure out what is going wrong. We have our own open base table view controller class in a framework that conforms to UITableViewDatasource and UITableViewDelegate. In Swift 4 the subclasses could simply implement any of those delegate methods and they would work as expected. With Swift 5 and the latest Xcode this has all changed and any signature from those protocols that isn't explicitly written out in the base class are completely ignored, causing all kinds of strange bugs.Our base class implementation looks something like this:open class OsuTableViewController: OsuViewController, UITableViewDelegate, UITableViewDataSource { public let tableView = UITableView(frame: .zero, style: .grouped) public var tableSections = [OsuTableSection]() override open func viewDidLoad() { super.viewDidLoad() tableView.datasource = self tableView.delegate = self } open func numberOfSections(in tableView: UITableView) -> Int { return tab
Replies
3
Boosts
0
Views
4.2k
Activity
Apr ’19
Sort on the @relationship property wrapper
Hello Developer Support, Let's say I have the following models. Is there a way to have dataSet pre-sorted by swiftData? @Model Struct Capture { let someInfo: String @Relationship(.unique, deleteRule: .cascade) // some way of sorting by CoolPoint.date let dataSet: [CoolPoint] } @Model struct CoolPoint { let point: Int let date: Date }
Replies
0
Boosts
0
Views
746
Activity
Jan ’24
Reply to How to handle required @relationship optionals in SwiftData CloudKit?
SwiftData + CloudKit uses NSPersistentCloudKitContainer under the hood, which requires all relationships must be optional. For more information, see Creating a Core Data Model for CloudKit. The requirement exists because of the latency of the synchronization: When you create an object graph in device A, which is being synchronized to device B, the system doesn't guarantee to synchronize the whole graph all at once. As a result, it's possible that an object is synchronized but its relationship is not. This situation is expressed as the relationship being nil. By checking if the relationship is nil, the app instance running on device B can consume the object appropriately. In your case, wrapping a relationship with a computed property to return an empty array if nil makes sense to me, if the other part of your app prefers to consume an empty array. It doesn't matter if the data is big or small. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Oct ’25