Search results for

“SwiftData inheritance relationship”

4,986 results found

Post

Replies

Boosts

Views

Activity

SwiftData does not retrieve my inverse one-to-many Relationship
Here is my models: import SwiftData @Model final public class FirstModel { let name: String @Relationship(deleteRule: .cascade, inverse: SecondModel.parent) var children = [SecondModel]() init(name: String) { self.name = name } } @Model final public class SecondModel { let parent: FirstModel let name: String @Relationship(deleteRule: .cascade, inverse: ThirdModel.parent) var children = [ThirdModel]() init(name: String, parent: FirstModel) { self.name = name self.parent = parent } } @Model final public class ThirdModel { let parent: SecondModel let name: String init(name: String, parent: SecondModel) { self.name = name self.parent = parent } } Then I create my model entries: let schema = Schema([ FirstModel.self, SecondModel.self, ThirdModel.self ]) let container = try ModelContainer(for: schema) let context = ModelContext(container) let firstModel = FirstModel(name: my first model) let secondModel = SecondModel(name: my second model, parent: firstModel) let thirdModel = ThirdModel(n
1
0
1.1k
May ’24
Xcode 16.0. SwiftData Schema Fatal error: Inverse already set to another relationship
Hi, after upgrading to Xcode 16.0 I encountered the fact that when loading preview, I get an error: Fatal error: Inverse already set to another relationship - secondAccount - cannot assign to - firstAccount import Foundation import SwiftData import SwiftUI @Model class TransactionModel { @Relationship(inverse: AccountModel.accountTransactions) var firstAccount: AccountModel? /// <- here @Relationship(inverse: AccountModel.accountTransactions) var secondAccount: AccountModel? /// <- here @Relationship(inverse: DebtorModel.transactions) var debtor: DebtorModel? @Relationship(inverse: CategoryModel.transactions) var category: CategoryModel? init(account: AccountModel? = nil, secondAccount: AccountModel? = nil, debtor: DebtorModel? = nil, category: CategoryModel? = nil) { self.firstAccount = account self.secondAccount = secondAccount self.debtor = debtor self.category = category } } import SwiftData import SwiftUI @Model final public class Accou
1
0
709
Sep ’24
Save SwiftData object for model with one to many relationship
First the Model: import Foundation import SwiftData //Model for Earned Value Analysis @Model final class CostReport{ var aCWP: Double //Actual Cost of Work Performed var bCWP: Double //Budgeted Cost of Work Performed var bCWS: Double // Budgeted Cost of Work Scheduled var cumACWP: Double// Cumlative Actual Cost of Work Performed var cumBCWP: Double // Cumlative Budgeted Cost of Work Performed var cumBCWS: Double // Cumlative Budgeted Cost of Work Scheduled var startDateOfPeriod: Date var endDateOfPeriod: Date var contract: Contract? init(aCWP: Double = 0.0, bCWP: Double = 0.0, bCWS: Double = 0.0, cumACWP: Double = 0.0, cumBCWP: Double = 0.0, cumBCWS: Double = 0.0, startDateOfPeriod: Date = .now, endDateOfPeriod: Date = .now, contract: Contract) { self.aCWP = aCWP self.bCWP = bCWP self.bCWS = bCWS self.cumACWP = cumACWP self.cumBCWP = cumBCWP self.cumBCWS = cumBCWS self.startDateOfPeriod = startDateOfPeriod self.endDateOfPeriod = endDateOfPeriod self.contract = contract } } @Model //Model for Contract
4
0
246
Sep ’25
Reply to @Relationship crash on ios17.5 but not on ios18
If you can share your code that triggered the exception, I may be able to take a look and comment. iOS 18 fixed some SwiftData bugs, but without looking into the relevant code, folks probably can't tell if the issue you described is indeed a bug and if there is a workaround for it (on iOS 17). Maybe worth mentioning, when working with SwiftData relationships, a rule of thumb is to insert the model objects to the model context before related them, as discussed in SwiftData inverse relationship not updating. Failing to do so may trigger an exception, which is pretty similar to what you showed, when the relationship is accessed. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Sep ’24
Help with 2 way relationships with classes (SwiftData / cloudkit)
Hi I’m having real problems trying to get a simple “to do” type app working with cloudkit. It works fine with SwiftData but as soon as I add CloudKit I get lots of “container not found errors “ which I think relates to the relationships between my classes. If I strip out the group sort order class it works fine. I’ve stripped the app back to basics to test - I want to be able to add a “task” (task data) to a “group list “ (group data) also also store the position of the task in that list (group sort order) as there may be lots of tasks in a list. The same task could also be in a different group list with a different position in the list (so another entry and value in group sort order) .. why does the following not work?? any help appreciated! // TaskData.swift // TaskOutApp // import Foundation import SwiftData `@Model class TaskData: Identifiable, Equatable { var id = UUID() var title: String = No Title var isDone: Bool = false var isToday: Bool = false var creationDate: Date = Dat
3
0
742
Sep ’24
required keyword, initializers, and class inheritance
On page https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID231, a page on Initialization, at the section called Required Initializers, I find the following sentence:Write therequired modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer:What does it mean to implement the initializer? Does that mean I have to explicitly write code for the inherited initializer, or could I consider the inherited initializers implemented without writing additional code in the subclass by virtue of inheritance? I thought initializers are automatically inherited by subclasses from the superclass by virtue of inheritance, without having to write explicit code for them in the subclass.
1
0
691
May ’16
Does SWIFT_ACTIVE_COMPILATION_CONDITIONS and inherited from included files work?
I have a Debug.xcconfig file: SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG I have a AppUAT.Debug.xcconfig file that includes the above file and tries to use inheritance to only add a new value: #include Debug.xcconfig SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) UAT Build Settings will only show: Active Compilation Conditions == UAT I put the quotes there to show $(inherited) is nil and the space is literally carried over from the space after $(inherited). What am I doing wrong?
1
0
3.3k
Oct ’20
What Is The Recommended View Hierarchy for SwiftData Many-Many Relationships?
I am building an app where tasks can be shown on multiple selected days. The user will select a day and the available tasks should be shown. I am struggling to build a view hierarchy where the visible tasks will update when added to the currently shown day. A simplified model looks like the below: @Model final class Day { private(set) var dayID: UUID = UUID() var show: [Item]? } @Model final class Item { private(set) var itemID: UUID = UUID() @Relationship(inverse: Day.show) var showDays: [Day]? } I have a view representing a day, and I would like it to display a list of associated tasks below. The view doesn't update if I list a day's tasks directly, e.g. List(day.show) {} I get a compile error if I build a sub view that queries Item using the day's show array: let show = day.show ?? [] let predicate = #Predicate { item in show.contains(where: { $0.itemID == item.itemID }) } I get runtime error if I flip the predicate: let dayID = day.dayID let predicate: Predicate = #Predicate { item in item.showDa
1
0
393
Oct ’24
SwiftData does not retrieve my inverse one-to-many Relationship
Here is my models: import SwiftData @Model final public class FirstModel { let name: String @Relationship(deleteRule: .cascade, inverse: SecondModel.parent) var children = [SecondModel]() init(name: String) { self.name = name } } @Model final public class SecondModel { let parent: FirstModel let name: String @Relationship(deleteRule: .cascade, inverse: ThirdModel.parent) var children = [ThirdModel]() init(name: String, parent: FirstModel) { self.name = name self.parent = parent } } @Model final public class ThirdModel { let parent: SecondModel let name: String init(name: String, parent: SecondModel) { self.name = name self.parent = parent } } Then I create my model entries: let schema = Schema([ FirstModel.self, SecondModel.self, ThirdModel.self ]) let container = try ModelContainer(for: schema) let context = ModelContext(container) let firstModel = FirstModel(name: my first model) let secondModel = SecondModel(name: my second model, parent: firstModel) let thirdModel = ThirdModel(n
Replies
1
Boosts
0
Views
1.1k
Activity
May ’24
Xcode 16.0. SwiftData Schema Fatal error: Inverse already set to another relationship
Hi, after upgrading to Xcode 16.0 I encountered the fact that when loading preview, I get an error: Fatal error: Inverse already set to another relationship - secondAccount - cannot assign to - firstAccount import Foundation import SwiftData import SwiftUI @Model class TransactionModel { @Relationship(inverse: AccountModel.accountTransactions) var firstAccount: AccountModel? /// <- here @Relationship(inverse: AccountModel.accountTransactions) var secondAccount: AccountModel? /// <- here @Relationship(inverse: DebtorModel.transactions) var debtor: DebtorModel? @Relationship(inverse: CategoryModel.transactions) var category: CategoryModel? init(account: AccountModel? = nil, secondAccount: AccountModel? = nil, debtor: DebtorModel? = nil, category: CategoryModel? = nil) { self.firstAccount = account self.secondAccount = secondAccount self.debtor = debtor self.category = category } } import SwiftData import SwiftUI @Model final public class Accou
Replies
1
Boosts
0
Views
709
Activity
Sep ’24
Save SwiftData object for model with one to many relationship
First the Model: import Foundation import SwiftData //Model for Earned Value Analysis @Model final class CostReport{ var aCWP: Double //Actual Cost of Work Performed var bCWP: Double //Budgeted Cost of Work Performed var bCWS: Double // Budgeted Cost of Work Scheduled var cumACWP: Double// Cumlative Actual Cost of Work Performed var cumBCWP: Double // Cumlative Budgeted Cost of Work Performed var cumBCWS: Double // Cumlative Budgeted Cost of Work Scheduled var startDateOfPeriod: Date var endDateOfPeriod: Date var contract: Contract? init(aCWP: Double = 0.0, bCWP: Double = 0.0, bCWS: Double = 0.0, cumACWP: Double = 0.0, cumBCWP: Double = 0.0, cumBCWS: Double = 0.0, startDateOfPeriod: Date = .now, endDateOfPeriod: Date = .now, contract: Contract) { self.aCWP = aCWP self.bCWP = bCWP self.bCWS = bCWS self.cumACWP = cumACWP self.cumBCWP = cumBCWP self.cumBCWS = cumBCWS self.startDateOfPeriod = startDateOfPeriod self.endDateOfPeriod = endDateOfPeriod self.contract = contract } } @Model //Model for Contract
Replies
4
Boosts
0
Views
246
Activity
Sep ’25
Reply to How to subclass with SwiftData?
I'm fairly certain that SwiftData simply doesn't support inheritance (at least not in beta 1). I posted about that a few days ago, and I've also filed FB12336064 asking for the feature to be added.
Replies
Boosts
Views
Activity
Jun ’23
Reply to SwiftData does not cascade delete
Not working for me neighter, SwiftData do not cascade delete relationships.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’23
Reply to @Relationship crash on ios17.5 but not on ios18
If you can share your code that triggered the exception, I may be able to take a look and comment. iOS 18 fixed some SwiftData bugs, but without looking into the relevant code, folks probably can't tell if the issue you described is indeed a bug and if there is a workaround for it (on iOS 17). Maybe worth mentioning, when working with SwiftData relationships, a rule of thumb is to insert the model objects to the model context before related them, as discussed in SwiftData inverse relationship not updating. Failing to do so may trigger an exception, which is pretty similar to what you showed, when the relationship is accessed. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Sep ’24
Reply to SwiftData Query relationships not working
You need to add @Relationship to var students: [Student] in Department model, also: import Foundation import SwiftData @Model class Department { ... @Relationship var students: [Student] ... }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’23
Help with 2 way relationships with classes (SwiftData / cloudkit)
Hi I’m having real problems trying to get a simple “to do” type app working with cloudkit. It works fine with SwiftData but as soon as I add CloudKit I get lots of “container not found errors “ which I think relates to the relationships between my classes. If I strip out the group sort order class it works fine. I’ve stripped the app back to basics to test - I want to be able to add a “task” (task data) to a “group list “ (group data) also also store the position of the task in that list (group sort order) as there may be lots of tasks in a list. The same task could also be in a different group list with a different position in the list (so another entry and value in group sort order) .. why does the following not work?? any help appreciated! // TaskData.swift // TaskOutApp // import Foundation import SwiftData `@Model class TaskData: Identifiable, Equatable { var id = UUID() var title: String = No Title var isDone: Bool = false var isToday: Bool = false var creationDate: Date = Dat
Replies
3
Boosts
0
Views
742
Activity
Sep ’24
Reply to Bug? SwiftData + inheritance + optional many-to-one relationship
Hello, I am planning a project using SwiftData and inheritance with CloudKit but I am quite concerned about running into this bug. When I click the link to the FB ID above, it comes up empty as do searches for it. Does anyone know the current status?
Replies
Boosts
Views
Activity
Jan ’26
Reply to SwiftData crashed when I insert (XCode 15.0)
In Page, try this @Relationship(deleteRule: .cascade, inverse: Line.page) var pages: [Page] In Line, simply try the following var page: Page No need for @Relationship here as SwiftData infers this.
Replies
Boosts
Views
Activity
Nov ’23
required keyword, initializers, and class inheritance
On page https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID231, a page on Initialization, at the section called Required Initializers, I find the following sentence:Write therequired modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer:What does it mean to implement the initializer? Does that mean I have to explicitly write code for the inherited initializer, or could I consider the inherited initializers implemented without writing additional code in the subclass by virtue of inheritance? I thought initializers are automatically inherited by subclasses from the superclass by virtue of inheritance, without having to write explicit code for them in the subclass.
Replies
1
Boosts
0
Views
691
Activity
May ’16
Does SWIFT_ACTIVE_COMPILATION_CONDITIONS and inherited from included files work?
I have a Debug.xcconfig file: SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG I have a AppUAT.Debug.xcconfig file that includes the above file and tries to use inheritance to only add a new value: #include Debug.xcconfig SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) UAT Build Settings will only show: Active Compilation Conditions == UAT I put the quotes there to show $(inherited) is nil and the space is literally carried over from the space after $(inherited). What am I doing wrong?
Replies
1
Boosts
0
Views
3.3k
Activity
Oct ’20
What Is The Recommended View Hierarchy for SwiftData Many-Many Relationships?
I am building an app where tasks can be shown on multiple selected days. The user will select a day and the available tasks should be shown. I am struggling to build a view hierarchy where the visible tasks will update when added to the currently shown day. A simplified model looks like the below: @Model final class Day { private(set) var dayID: UUID = UUID() var show: [Item]? } @Model final class Item { private(set) var itemID: UUID = UUID() @Relationship(inverse: Day.show) var showDays: [Day]? } I have a view representing a day, and I would like it to display a list of associated tasks below. The view doesn't update if I list a day's tasks directly, e.g. List(day.show) {} I get a compile error if I build a sub view that queries Item using the day's show array: let show = day.show ?? [] let predicate = #Predicate { item in show.contains(where: { $0.itemID == item.itemID }) } I get runtime error if I flip the predicate: let dayID = day.dayID let predicate: Predicate = #Predicate { item in item.showDa
Replies
1
Boosts
0
Views
393
Activity
Oct ’24
Reply to Fetching data with relationships directly faults the relationships even when not accessed
The only way around it I've found is to create an intermediate 1:1 model that contains the 1:N relationship since SwiftData won't query 1:1 relationships by default.
Replies
Boosts
Views
Activity
Jul ’25
Reply to Added new field to swiftdata model, now crashes
I'v seen the same error today. This seems to be a bug of SwiftData. I don't think SchemaMigrationPlan is ready for production. SwiftData/BackingData.swift:432: Fatal error: Expected only Arrays for Relationships - String
Replies
Boosts
Views
Activity
Feb ’24