Search results for

“SwiftData inheritance relationship”

4,986 results found

Post

Replies

Boosts

Views

Activity

Inheritance in SwiftData — Fatal error: Never access a full future backing data
I'm implementing SwiftData with inheritance in an app. I have an Entity class with a property name. This class is inherited by two other classes: Store and Person. The Entity model has a one-to-many relationship with a Transaction class. I can list all my Entity models in a List with a @Query annotation without a problem. However, then I try to access the name property of an Entity from a Transaction relationship, the app crashes with the following error: Thread 1: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0x96530ce28d41eb63 ))) with Optional(F07E7E23-F8F0-4CC0-B282-270B5EDDC7F3) From my attempts to fix the issue, I noticed that: The crash seems related to the relationships with classes that has inherit from another class, since it only happens there. When I create new data, I can usually acces
1
0
272
Sep ’25
swiftdata relationship xcode15b3 ios17
I can't find a way to make this relationship, Anyone to tell me what am i doing wrong please ? import SwiftUI import SwiftData @Model class Model1 { @Attribute(.unique) var id:String var date:Date @Relationship(.cascade) var model2:[Model2] init() { self.id = UUID().uuidString self.date = Date() self.model2 = [] } } @Model class Model2 { @Attribute(.unique) var id:String var date:Date @Relationship(inverse: Model1.model2) var model1:Model1 init( model1:Model1 ) { self.id = UUID().uuidString self.date = Date() self.model1 = model1 } } struct view1:View { @Environment(.modelContext) private var db var body: some View { Text(TapMe) .onTapGesture { let m1 = Model1() db.insert(m1) let m2 = Model2(model1: m1) db.insert(m2) } } } Error : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'm1' between objects in different contexts (source = (entity: Model2; id: 0x282896ca0 ; data: { date = 2023-07-1
1
0
1.4k
Jul ’23
SwiftData Relationship Persistence
I'm still getting started with SwiftData and having trouble understanding how to persist a model with a relationship in a container. I've tried to distill the example below to be pretty simple. Here are some example model definitions. I would like to be able to define a n Item. That Item can have SubItems related to it in a one-to-many fashion. Each SubItem is required to be attached to a parent Item. final class Item { var name: String var subitems: [Item]? init( name: String, subitems: [SubItem]? = nil ) { self.name = name } } @Model final class SubItem { var name: String init(name: String) { self.name = name } } In my app I am then defining a preview container with some pre-saved data already. Item( name: item1, subitems: [ SubItem(name: subItemA), SubItem(name: subItemB) ] ), Item( name: item2, subitems: [ SubItem(name: subItemC), SubItem(name: subItemD) ] ) ] @MainActor let PreviewContainer: ModelContainer = { do { let schema = Schema([ Item.self, SubItem.self, ]) let container = try Mo
1
0
490
Sep ’24
SwiftData #Predicate cannot test for nil relationship
In Xcode Version 15.0 beta (15A5160n) this predicate will not build: let thisPredicate = #Predicate { ($0.myStore != nil) } The definition of .myStore in the Ledgers class is: var myStore: Stores? an optional relationship. Is there anyway to test for nil optional relationships in SwiftData? The inverse relationship in Stores is: @Relationship(.deny, inverse: Ledgers.myStore) var myReceipts: [Ledgers]
10
0
3.1k
Jun ’23
SwiftData Relationship Delete Not Working (SwiftData/PersistentModel.swift:359)
SwiftData delete isn't working, when I attempt to delete a model, my app crashes and I get the following error: SwiftData/PersistentModel.swift:359: Fatal error: Cannot remove My_App.Model2 from relationship Relationship - name: model2, options: [], valueType: Model2, destination: Model2, inverseName: models3, inverseKeypath: Optional(Model2.models3) on My_App.Model3 because an appropriate default value is not configured. I get that it's saying I don't have a default value, but why do I need one? Isn't @Relationship .cascade automatically deleting the associated models? And onto of that, why is the error occurring within the do block, shouldn't it be caught by the catch, and printed? I have put together a sample project below. import SwiftUI import SwiftData @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: Model3.self) } } } @Model class Model1 { var name: String @Relationship(deleteRule: .cascade,
1
0
1.1k
Jan ’25
SwiftData Query and optional relationships
Xcode 16.2 (16C5032a) FB16300857 Consider the following SwiftData model objects (only the relevant portions are shown) (note that all relationships are optional because eventually this app will use CloudKit): @Model final public class Team { public var animal: Animal? public var handlers: [Handler]? ... } @Model final public class Animal { public var callName: String public var familyName: String @Relationship(inverse: Team.animal) public var teams: [Team]? ... } @Model final public class Handler { public var givenName: String @Relationship(inverse: Team.handlers) public var teams: [Team]? } Now I want to display Team records in a list view, sorted by animal.familyName, animal.callName, and handlers.first.givenName. The following code crashes: struct TeamListView: View { @Query(sort: [SortDescriptor(Team.animal?.familyName), SortDescriptor(Team.animal?.callName), SortDescriptor(Team.handlers?.first?.givenName)]) var teams : [Team] var body: some View { List { ForEach(teams)
5
0
922
Jan ’25
SwiftData inverse relationship not updating
Given the code below the students array on the school is not being updated. Why? Since the relationship is explicit and non-optional I would expect this to work. import XCTest import SwiftData @Model class School { var name: String @Relationship(deleteRule: .cascade, inverse: Student.school) var students: [Student] init(name: String, students: [Student]) { self.name = name self.students = students } } @Model class Student { var name: String var school: School init(name: String, school: School) { self.name = name self.school = school } } final class Test: XCTestCase { func testScenario() throws { let modelContainer = try ModelContainer(for: School.self, Student.self ) let context = ModelContext(modelContainer) context.autosaveEnabled = false let school = School(name: school, students: []) context.insert(school) let student1 = Student(name: 1, school: school) let student2 = Student(name: 2, school: school) context.insert(student1) context.insert(student2) XCTAssertEqual(school.student
2
0
1k
Sep ’24
SwiftData: Unknown Relationship Type - nil
I'm getting an error: Unknown Relationship Type - nil when using SwiftData and CloudKit. I've searched for this on Google, but seems like one has experienced it before. This is on iOS 18. I have a Transaction model: @Model final class Transaction { var timestamp: Date = Date() var note: String = var cost: Cost? = nil var receipt: Receipt? = nil //Relationships var merchant: Merchant? = nil var category: TransactionCategory? = nil var tags: [Tag]? = nil init(timestamp: Date) { self.timestamp = timestamp } } This has a relationship with TransactionCategory. @Model final class TransactionCategory { var id = UUID() var name: String = //Relationship var transactions: [Transaction]? = [] init() { } init(name: String) { self.name = name } } I've tried using @Relationship here in TransactionCategory, but it didn't make a difference. There is Picker that allows you to select a Category. In this case I created a random one, which was inserted into the context and
1
0
615
Jul ’24
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
Crash when accessing relationship property of SwiftData model
I have two one-to-many models and I'm getting an unexplained crash when trying to access the relationship properties of the models. The reason for the error is as follows: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1038f4448) Xcode shows that the error occurs in the model's .getValue(for: .rows) method. This is my SwiftData model and other code: @Model class Row { var section: Section? init(section: Section? = nil) { self.section = section } init() { self.section = nil } } @Model class Section { @Relationship(.cascade, inverse: Row.section) var rows: [Row] = [] init(rows: [Row]) { self.rows = rows } } class ViewController: UIViewController { var container: ModelContainer? override func viewDidLoad() { super.viewDidLoad() do { container = try ModelContainer(for: [Section.self, Row.self]) } catch { print(error) } let row = Row() let section = Section(rows: [row]) section.rows.append(row) var myRows = section.rows //Accessing relationship properties causes crash print(hello)
1
0
792
Aug ’23
SwiftData Crash On Models With Relationships Deletion
It seems that when you delete model objects with a relationship there seems to be a crash occurring if the collection your observing is tied to a ForEach. Below is an example of the models in SwiftData @Model class Category { @Attribute(.unique) var title: String var items: [Item]? init(title: String = ) { self.title = title } } The relationship is defined between the category and the item @Model final class Item { var title: String var timestamp: Date var isCritical: Bool var isCompleted: Bool @Relationship(.nullify, inverse: Category.items) var category: Category? init(title: String = , timestamp: Date = .now, isCritical: Bool = false, isCompleted: Bool = false) { self.title = title self.timestamp = timestamp self.isCritical = isCritical self.isCompleted = isCompleted } } So if I were to create a category individually so i can later tag it to an item i'm just following the standard proceedure of creating a category and inserting it into the context as you can see below Bu
5
0
3.6k
Jun ’23
Reply to XCode 8.1 -> duplicate symbol __TMRbBp
One solution is to add Final to the class definition. I resolved the problem for one of my projectBut for another my project, I found the class inherit relationship is compicated, for example classA inherit classB, classB inherit classC, as no way to add final to classB, the problem can not be resolved. If no other solution, I may have to change the class inherit relationship to just two levels.
Oct ’16
SwiftData migration crashes when working with relationships
The following complex migration consistently crashes the app with the following error: SwiftData/PersistentModel.swift:726: Fatal error: What kind of backing data is this? SwiftData._KKMDBackingData My app relies on a complex migration that involves these optional 1 to n relationships. Theoretically I could not assign the relationships in the willMigrate block but afterwards I am not able to tell which list and items belonged together. Steps to reproduce: Run project Change typealias CurrentSchema to ItemSchemaV2 instead of ItemSchemaV1. Run project again -> App crashes My setup: Xcode Version 16.2 (16C5032a) MacOS Sequoia 15.4 iPhone 12 with 18.3.2 (22D82) Am I doing something wrong or did I stumble upon a bug? I have a demo Xcode project ready but I could not upload it here so I put the code below. Thanks for your help typealias CurrentSchema = ItemSchemaV1 typealias ItemList = CurrentSchema.ItemList typealias Item = CurrentSchema.Item @main struct SwiftDataMigrationAp
1
0
183
Apr ’25
Relationship Fault not happening in SwiftData
I have two models a Person and a Possession the Person model has a one to many relationship to the Possession model. meaning each possession can only have one person but a person can have multiple possessions. I have set my model like the following Person: @Model class Person { @Attribute(.unique) let personID: String @Relationship(.cascade, inverse: Possession.person) var possetions: [Possession]? init(id: String, possessions: [Possession]) { self.personID = id self.possetions = possessions } } Possession: @Model class Possession { @Attribute(.unique) let id: String let name: String? var person: Person? init(id: String, name: String, person: Person) { self.id = id self.name = name self.person = person } } If i set a breakpoint i see that all the posessions are loaded into the memory this is something i do not want to happen. In Core Data we get a relationship fault however, i am not seeing the same behavior in SwiftData. here's how my view is implemented struct ContentView
0
0
924
Jul ’23
SwiftData @Model: Optional to-many relationship is never nil at runtime
Hi all, I’m trying to understand SwiftData’s runtime semantics around optional to-many relationships, especially in the context of CloudKit-backed models. I ran into behavior that surprised me, and I’d like to confirm whether this is intended design or a potential issue / undocumented behavior. Minimal example import SwiftUI import SwiftData @Model class Node { var children: [Node]? = nil var parent: Node? = nil init(children: [Node]? = nil, parent: Node? = nil) { self.children = children self.parent = parent print(self.children == nil) } } struct ContentView: View { var body: some View { Button(Create) { _ = Node(children: nil) } } } Observed behavior If @Model is not used, children == nil prints true as expected. If @Model is used, children == nil prints false. Inspecting the macro expansion, it appears SwiftData initializes relationship storage using backing data placeholders and normalizes to-many relationships into empty collections at runtime, even w
1
0
370
Dec ’25
Inheritance in SwiftData — Fatal error: Never access a full future backing data
I'm implementing SwiftData with inheritance in an app. I have an Entity class with a property name. This class is inherited by two other classes: Store and Person. The Entity model has a one-to-many relationship with a Transaction class. I can list all my Entity models in a List with a @Query annotation without a problem. However, then I try to access the name property of an Entity from a Transaction relationship, the app crashes with the following error: Thread 1: Fatal error: Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0x96530ce28d41eb63 ))) with Optional(F07E7E23-F8F0-4CC0-B282-270B5EDDC7F3) From my attempts to fix the issue, I noticed that: The crash seems related to the relationships with classes that has inherit from another class, since it only happens there. When I create new data, I can usually acces
Replies
1
Boosts
0
Views
272
Activity
Sep ’25
swiftdata relationship xcode15b3 ios17
I can't find a way to make this relationship, Anyone to tell me what am i doing wrong please ? import SwiftUI import SwiftData @Model class Model1 { @Attribute(.unique) var id:String var date:Date @Relationship(.cascade) var model2:[Model2] init() { self.id = UUID().uuidString self.date = Date() self.model2 = [] } } @Model class Model2 { @Attribute(.unique) var id:String var date:Date @Relationship(inverse: Model1.model2) var model1:Model1 init( model1:Model1 ) { self.id = UUID().uuidString self.date = Date() self.model1 = model1 } } struct view1:View { @Environment(.modelContext) private var db var body: some View { Text(TapMe) .onTapGesture { let m1 = Model1() db.insert(m1) let m2 = Model2(model1: m1) db.insert(m2) } } } Error : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'm1' between objects in different contexts (source = (entity: Model2; id: 0x282896ca0 ; data: { date = 2023-07-1
Replies
1
Boosts
0
Views
1.4k
Activity
Jul ’23
SwiftData Relationship Persistence
I'm still getting started with SwiftData and having trouble understanding how to persist a model with a relationship in a container. I've tried to distill the example below to be pretty simple. Here are some example model definitions. I would like to be able to define a n Item. That Item can have SubItems related to it in a one-to-many fashion. Each SubItem is required to be attached to a parent Item. final class Item { var name: String var subitems: [Item]? init( name: String, subitems: [SubItem]? = nil ) { self.name = name } } @Model final class SubItem { var name: String init(name: String) { self.name = name } } In my app I am then defining a preview container with some pre-saved data already. Item( name: item1, subitems: [ SubItem(name: subItemA), SubItem(name: subItemB) ] ), Item( name: item2, subitems: [ SubItem(name: subItemC), SubItem(name: subItemD) ] ) ] @MainActor let PreviewContainer: ModelContainer = { do { let schema = Schema([ Item.self, SubItem.self, ]) let container = try Mo
Replies
1
Boosts
0
Views
490
Activity
Sep ’24
SwiftData #Predicate cannot test for nil relationship
In Xcode Version 15.0 beta (15A5160n) this predicate will not build: let thisPredicate = #Predicate { ($0.myStore != nil) } The definition of .myStore in the Ledgers class is: var myStore: Stores? an optional relationship. Is there anyway to test for nil optional relationships in SwiftData? The inverse relationship in Stores is: @Relationship(.deny, inverse: Ledgers.myStore) var myReceipts: [Ledgers]
Replies
10
Boosts
0
Views
3.1k
Activity
Jun ’23
SwiftData Relationship Delete Not Working (SwiftData/PersistentModel.swift:359)
SwiftData delete isn't working, when I attempt to delete a model, my app crashes and I get the following error: SwiftData/PersistentModel.swift:359: Fatal error: Cannot remove My_App.Model2 from relationship Relationship - name: model2, options: [], valueType: Model2, destination: Model2, inverseName: models3, inverseKeypath: Optional(Model2.models3) on My_App.Model3 because an appropriate default value is not configured. I get that it's saying I don't have a default value, but why do I need one? Isn't @Relationship .cascade automatically deleting the associated models? And onto of that, why is the error occurring within the do block, shouldn't it be caught by the catch, and printed? I have put together a sample project below. import SwiftUI import SwiftData @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: Model3.self) } } } @Model class Model1 { var name: String @Relationship(deleteRule: .cascade,
Replies
1
Boosts
0
Views
1.1k
Activity
Jan ’25
SwiftData Query and optional relationships
Xcode 16.2 (16C5032a) FB16300857 Consider the following SwiftData model objects (only the relevant portions are shown) (note that all relationships are optional because eventually this app will use CloudKit): @Model final public class Team { public var animal: Animal? public var handlers: [Handler]? ... } @Model final public class Animal { public var callName: String public var familyName: String @Relationship(inverse: Team.animal) public var teams: [Team]? ... } @Model final public class Handler { public var givenName: String @Relationship(inverse: Team.handlers) public var teams: [Team]? } Now I want to display Team records in a list view, sorted by animal.familyName, animal.callName, and handlers.first.givenName. The following code crashes: struct TeamListView: View { @Query(sort: [SortDescriptor(Team.animal?.familyName), SortDescriptor(Team.animal?.callName), SortDescriptor(Team.handlers?.first?.givenName)]) var teams : [Team] var body: some View { List { ForEach(teams)
Replies
5
Boosts
0
Views
922
Activity
Jan ’25
SwiftData inverse relationship not updating
Given the code below the students array on the school is not being updated. Why? Since the relationship is explicit and non-optional I would expect this to work. import XCTest import SwiftData @Model class School { var name: String @Relationship(deleteRule: .cascade, inverse: Student.school) var students: [Student] init(name: String, students: [Student]) { self.name = name self.students = students } } @Model class Student { var name: String var school: School init(name: String, school: School) { self.name = name self.school = school } } final class Test: XCTestCase { func testScenario() throws { let modelContainer = try ModelContainer(for: School.self, Student.self ) let context = ModelContext(modelContainer) context.autosaveEnabled = false let school = School(name: school, students: []) context.insert(school) let student1 = Student(name: 1, school: school) let student2 = Student(name: 2, school: school) context.insert(student1) context.insert(student2) XCTAssertEqual(school.student
Replies
2
Boosts
0
Views
1k
Activity
Sep ’24
SwiftData: Unknown Relationship Type - nil
I'm getting an error: Unknown Relationship Type - nil when using SwiftData and CloudKit. I've searched for this on Google, but seems like one has experienced it before. This is on iOS 18. I have a Transaction model: @Model final class Transaction { var timestamp: Date = Date() var note: String = var cost: Cost? = nil var receipt: Receipt? = nil //Relationships var merchant: Merchant? = nil var category: TransactionCategory? = nil var tags: [Tag]? = nil init(timestamp: Date) { self.timestamp = timestamp } } This has a relationship with TransactionCategory. @Model final class TransactionCategory { var id = UUID() var name: String = //Relationship var transactions: [Transaction]? = [] init() { } init(name: String) { self.name = name } } I've tried using @Relationship here in TransactionCategory, but it didn't make a difference. There is Picker that allows you to select a Category. In this case I created a random one, which was inserted into the context and
Replies
1
Boosts
0
Views
615
Activity
Jul ’24
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
Crash when accessing relationship property of SwiftData model
I have two one-to-many models and I'm getting an unexplained crash when trying to access the relationship properties of the models. The reason for the error is as follows: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1038f4448) Xcode shows that the error occurs in the model's .getValue(for: .rows) method. This is my SwiftData model and other code: @Model class Row { var section: Section? init(section: Section? = nil) { self.section = section } init() { self.section = nil } } @Model class Section { @Relationship(.cascade, inverse: Row.section) var rows: [Row] = [] init(rows: [Row]) { self.rows = rows } } class ViewController: UIViewController { var container: ModelContainer? override func viewDidLoad() { super.viewDidLoad() do { container = try ModelContainer(for: [Section.self, Row.self]) } catch { print(error) } let row = Row() let section = Section(rows: [row]) section.rows.append(row) var myRows = section.rows //Accessing relationship properties causes crash print(hello)
Replies
1
Boosts
0
Views
792
Activity
Aug ’23
SwiftData Crash On Models With Relationships Deletion
It seems that when you delete model objects with a relationship there seems to be a crash occurring if the collection your observing is tied to a ForEach. Below is an example of the models in SwiftData @Model class Category { @Attribute(.unique) var title: String var items: [Item]? init(title: String = ) { self.title = title } } The relationship is defined between the category and the item @Model final class Item { var title: String var timestamp: Date var isCritical: Bool var isCompleted: Bool @Relationship(.nullify, inverse: Category.items) var category: Category? init(title: String = , timestamp: Date = .now, isCritical: Bool = false, isCompleted: Bool = false) { self.title = title self.timestamp = timestamp self.isCritical = isCritical self.isCompleted = isCompleted } } So if I were to create a category individually so i can later tag it to an item i'm just following the standard proceedure of creating a category and inserting it into the context as you can see below Bu
Replies
5
Boosts
0
Views
3.6k
Activity
Jun ’23
Reply to XCode 8.1 -> duplicate symbol __TMRbBp
One solution is to add Final to the class definition. I resolved the problem for one of my projectBut for another my project, I found the class inherit relationship is compicated, for example classA inherit classB, classB inherit classC, as no way to add final to classB, the problem can not be resolved. If no other solution, I may have to change the class inherit relationship to just two levels.
Replies
Boosts
Views
Activity
Oct ’16
SwiftData migration crashes when working with relationships
The following complex migration consistently crashes the app with the following error: SwiftData/PersistentModel.swift:726: Fatal error: What kind of backing data is this? SwiftData._KKMDBackingData My app relies on a complex migration that involves these optional 1 to n relationships. Theoretically I could not assign the relationships in the willMigrate block but afterwards I am not able to tell which list and items belonged together. Steps to reproduce: Run project Change typealias CurrentSchema to ItemSchemaV2 instead of ItemSchemaV1. Run project again -> App crashes My setup: Xcode Version 16.2 (16C5032a) MacOS Sequoia 15.4 iPhone 12 with 18.3.2 (22D82) Am I doing something wrong or did I stumble upon a bug? I have a demo Xcode project ready but I could not upload it here so I put the code below. Thanks for your help typealias CurrentSchema = ItemSchemaV1 typealias ItemList = CurrentSchema.ItemList typealias Item = CurrentSchema.Item @main struct SwiftDataMigrationAp
Replies
1
Boosts
0
Views
183
Activity
Apr ’25
Relationship Fault not happening in SwiftData
I have two models a Person and a Possession the Person model has a one to many relationship to the Possession model. meaning each possession can only have one person but a person can have multiple possessions. I have set my model like the following Person: @Model class Person { @Attribute(.unique) let personID: String @Relationship(.cascade, inverse: Possession.person) var possetions: [Possession]? init(id: String, possessions: [Possession]) { self.personID = id self.possetions = possessions } } Possession: @Model class Possession { @Attribute(.unique) let id: String let name: String? var person: Person? init(id: String, name: String, person: Person) { self.id = id self.name = name self.person = person } } If i set a breakpoint i see that all the posessions are loaded into the memory this is something i do not want to happen. In Core Data we get a relationship fault however, i am not seeing the same behavior in SwiftData. here's how my view is implemented struct ContentView
Replies
0
Boosts
0
Views
924
Activity
Jul ’23
SwiftData @Model: Optional to-many relationship is never nil at runtime
Hi all, I’m trying to understand SwiftData’s runtime semantics around optional to-many relationships, especially in the context of CloudKit-backed models. I ran into behavior that surprised me, and I’d like to confirm whether this is intended design or a potential issue / undocumented behavior. Minimal example import SwiftUI import SwiftData @Model class Node { var children: [Node]? = nil var parent: Node? = nil init(children: [Node]? = nil, parent: Node? = nil) { self.children = children self.parent = parent print(self.children == nil) } } struct ContentView: View { var body: some View { Button(Create) { _ = Node(children: nil) } } } Observed behavior If @Model is not used, children == nil prints true as expected. If @Model is used, children == nil prints false. Inspecting the macro expansion, it appears SwiftData initializes relationship storage using backing data placeholders and normalizes to-many relationships into empty collections at runtime, even w
Replies
1
Boosts
0
Views
370
Activity
Dec ’25