Search results for

“SwiftData inheritance relationship”

4,986 results found

Post

Replies

Boosts

Views

Activity

Reply to Issues with SwiftData One-to-Many Relationships
Yeah, I think the link @joadan provided most likely helps. Basically, SwiftData gets confused about how to infer the inverse relationship for Book.coontentPage and Book.pages because Page has only one relationship (Page.book). You can make the relationships clear by: Adding a new relationship to Page. Using @Relationship to explicitly specify the inverse relationship. In most of the cases, a relationship should have an inverse, but if you indeed don't have one in your case, pass nil to the inverse parameter of @Relationship. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Oct ’24
Reply to How do you create an inverse relationship to the same model in SwiftData
You need to add another property that can be used for the inverse relationship. You don't have to use this property, it's just to satisfy SwiftData's relationship requirements. For example: @Model final class Person { var name = @Relationship(inverse: Person.parent) var children: [Person]? @Relationship var parent: Person? // can make private if not using elsewhere }
Aug ’23
SwiftData does not persist change on relationship on iOS 18 beta 7
I came across of something I'm struggling to comprehend. I've got an iOS app based on SwiftUI and SwiftData + CloudKit. I wrote it using Xcode 15 and the target was iOS 17. Everything works fine in this environment, but after upgrading my phone to iOS 18 beta 7 something very strange started to happen with SwiftData on a physical device and in the simulator. Every time when data is updated, to be precise - when the relationship is modified, the change is reverted after 15 seconds! I've got the following settings on and nothing can be seen it's going on there in the logs -com.apple.CoreData.Logging.stderr 1 -com.apple.CoreData.CloudKitDebug 1 -com.apple.CoreData.SQLDebug 1 -com.apple.CoreData.ConcurrencyDebug 1 Here you are some simplified code extraction: @Model final public class Note: Identifiable, Hashable { public private(set) var uuid = UUID().uuidString var notification: Notification? ... } @Model final public class Notification: Identifiable, Hashable { var dateId: String =
3
0
1.6k
Aug ’24
Reply to SwiftData Class Inheritance
Check out the talk for SwiftData to get some insight on when you should harness Class Inheritance: https://developer.apple.com/videos/play/wwdc2025/291/ As for the depth and width of your Model Graph, it really depends on the full Schema and how the data is partitioned for the particular use cases.
Jul ’25
SwiftData: "Illegal attempt to establish a relationship 'item' between objects in different contexts
I have run into this SwiftData issue in multiple projects and have been able to replicate it by building off of the default SwiftData launch project. The original Item class: class Item { var timestamp: Date init(timestamp: Date) { self.timestamp = timestamp } } New MyItem class to replicate the error. Notice I nest an Item object inside MyItem: class MyItem { var name: String var item: Item init(name: String, item: Item) { self.name = name self.item = item } } I then build off of the default view for a SwiftData project. When the '+' button is pressed, a new list item for both Item and MyItem should appear in their appropriate sections. @Environment(.modelContext) private var modelContext @Query private var items: [Item] @Query private var myItems: [MyItem] var body: some View { NavigationSplitView { List { Section(All Items) { ForEach(items) { item in NavigationLink { Text(Item at (item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))) } label: { Text(item.tim
10
0
3.2k
Oct ’23
Reply to Model instance invalidated, backing data could no longer be found in the store
Update: the problem occurs when the app goes to the background, comes back, and SwiftUI tries to access relationships of SwiftData models that are direct input parameters to the View. My app has a function that opens a URL in a browser, it is when you press the Back button and return from the browser to the app that this fatal error tends to occur. When the app goes to the background, the BackingData of every relationship of this main model seems to get replaced with some subclass of FutureBackingData. Accessing them results in the fatal error above. Explicitly querying the relationship with @Query instead of relying on the relationship property seems to solve the problem. It would be great to have some documentation on how exactly SwiftData is behaving when the app goes to the background.
Topic: Programming Languages SubTopic: Swift Tags:
Dec ’24
Class inheritance with @Observable
Hi there! I'm quite new to SwiftUI; just wanted to know if there any chance to get observation for values in a class that inherits from another class, with the @Observable macro. I have the following class: @Observable class SourceData { var sourceHorizontalResolution: Int? var sourceVerticalResolution: Int? } This class is declared as @State in ContentView, and passed to views using .environment(): struct ContentView: View { @State private var sourceData = SourceData() var body: some View { HStack { VStack { SourceView() .environment(sourceData) //....More code } Finally, I have this class, that would inherit from SourceData: @Observable class PreviewData: SourceData { override init() { super.init() } var placeholderText: NSImage? {...} var previewImage: NSImage? { guard let sourceHorizontalResolution = sourceHorizontalResolution, let sourceVerticalResolution = sourceVerticalResolution else { return placeholderText } // More code here... return image } How can I observe for changes that occ
0
0
760
Oct ’23
SwiftData Migration Error: Missing Attribute Values on Mandatory Relationship
I'm currently working on a data model migration in Swift using a custom schema migration plan. My project involves migrating settings from one schema version to another (SchemaV1 to SchemaV2). Both schema versions include a class named FSViz, but with slightly different properties. In the newer schema, SchemaV2, I introduced a new property named textSettings of type TextSetting, replacing the textColor property from SchemaV1. Here's a simplified version of my migration code and class definitions: Model: extension SchemaV1 { @Model public final class FSViz { @Attribute(.unique) public let id: UUID public var textColor: TextColor init(textColor: TextColor) { self.id = UUID() self.textColor = textColor } } } extension SchemaV2 { @Model public final class FSViz { @Attribute(.unique) public let id: UUID public var textSettings: TextSetting init(textSettings: TextSetting) { self.id = UUID() self.textSettings = textSettings } } } initMyApp: public struct InitMyApp { static func makeInitialFSViz() -> FSViz? { FSVi
1
0
2.4k
Feb ’24
Do Generics not inherit initializers?
Four classes, first two normal, Two inherits from One, Two inherits One's initializers as expected.Second two are generic, Four<T> inherits from Three<T>, but everything else is the same. However Four doesn't inherit Three's initializer.Is that a bug or have I missed yet something else in the Swift book?public class One { public var value : Int public init( value : Int ) { self.value = value } } public class Two : One { public func foo() { print( value is (value) ) } } public class Three<T> { public var value : T public init( value : T ) { self.value = value } } public class Four<T> : Three<T> { public func foo() { print( value is (value) ) } } let x = Two( value : 123 ) // *** WORKS ****, Two inherits One's init(value:) method x.foo() let y = Four<Int>( value : 123 ) // Four<Int>' cannot be constructed because it has no accessible initializers y.foo()
0
0
981
Jun ’15
SwiftData serious bug with relationships and CloudKit in iOS 18.0 (Xcode 16 Beta)
Hi guys. Can someone please confirm this bug so I report it? The issue is that SwiftData relationships don't update the views in some specific situations on devices running iOS 18 Beta. One clear example is with CloudKit. I created a small example for testing. The following code creates two @models, one to store bands and another to store their records. The following code works with no issues. (You need to connect to a CloudKit container and test it on two devices) import SwiftUI import SwiftData struct ContentView: View { @Environment(.modelContext) private var modelContext @Query private var records: [Record] var body: some View { NavigationStack { List(records) { record in VStack(alignment: .leading) { Text(record.title) Text(record.band?.name ?? Undefined) } } .toolbar { ToolbarItem { Button(Add Record) { let randomNumber = Int.random(in: 1...100) let newBand = Band(name: New Band (randomNumber), records: nil) modelContext.insert(newBand) let newRecord = Record(title: New Record
3
0
888
Sep ’24
Inherit static library into appdlegate
We are trying to integrate an SDK developed in Objective C to iOS app developed in Swift. The SDK’s static library is inherited into App delegate class. Is this an accepted standard per Apple guidelines ? I wanted o check the same to avoid app rejection on submission to store.Eg : AppDelgate.swiftclass AppDelegate : MystaticLibrary,UIApplicationDelegate { //My code }MystaticLibrary-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //My Code }Thanks in advance!RegardsVignesh B RiOS Developer
0
0
245
Jan ’16
Resolving App Sandbox Inheritance Problems
This post is part of a cluster of posts related to the trusted execution system. If you found your way here directly, I recommend that you start at the top. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = eskimo + 1 + @ + apple.com Resolving App Sandbox Inheritance Problems If you’re creating a product with the App Sandbox enabled and it crashes with a trap within _libsecinit_appsandbox, it’s likely that you’re tripping over one of the following problems: Nonheritable entitlements Changing sandbox Nothing to inherit Nonheritable Entitlements The most common cause of this problem is also the most obscure. If you have a sandboxed app with an embedded program that you run as a child process, a crash in _libsecinit_appsandbox is most likely caused by the embedded program being signed with entitlements that can’t be inherited. Imagine an, SandboxInit, with an embedded helper tool, NotHeritable. When the app runs the helper tool as a child process
0
0
3.3k
May ’22
XCode Visual Inheritance
Hello everybody,Just a quick question. Does XCode support visual inheritance? For example if i have an NSWindow which I need to subclass (as it is a common window in the app i need to build), will it's conrols appear on the subclassed one?I've tried several stuff on this, but i could not manage to get it working. Maybe I am missing something. Does anybody have an idea or came across the same case? Any help would be appreciated!
1
0
219
Aug ’16
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
Reply to Issues with SwiftData One-to-Many Relationships
Yeah, I think the link @joadan provided most likely helps. Basically, SwiftData gets confused about how to infer the inverse relationship for Book.coontentPage and Book.pages because Page has only one relationship (Page.book). You can make the relationships clear by: Adding a new relationship to Page. Using @Relationship to explicitly specify the inverse relationship. In most of the cases, a relationship should have an inverse, but if you indeed don't have one in your case, pass nil to the inverse parameter of @Relationship. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Oct ’24
Reply to How do you create an inverse relationship to the same model in SwiftData
You need to add another property that can be used for the inverse relationship. You don't have to use this property, it's just to satisfy SwiftData's relationship requirements. For example: @Model final class Person { var name = @Relationship(inverse: Person.parent) var children: [Person]? @Relationship var parent: Person? // can make private if not using elsewhere }
Replies
Boosts
Views
Activity
Aug ’23
SwiftData does not persist change on relationship on iOS 18 beta 7
I came across of something I'm struggling to comprehend. I've got an iOS app based on SwiftUI and SwiftData + CloudKit. I wrote it using Xcode 15 and the target was iOS 17. Everything works fine in this environment, but after upgrading my phone to iOS 18 beta 7 something very strange started to happen with SwiftData on a physical device and in the simulator. Every time when data is updated, to be precise - when the relationship is modified, the change is reverted after 15 seconds! I've got the following settings on and nothing can be seen it's going on there in the logs -com.apple.CoreData.Logging.stderr 1 -com.apple.CoreData.CloudKitDebug 1 -com.apple.CoreData.SQLDebug 1 -com.apple.CoreData.ConcurrencyDebug 1 Here you are some simplified code extraction: @Model final public class Note: Identifiable, Hashable { public private(set) var uuid = UUID().uuidString var notification: Notification? ... } @Model final public class Notification: Identifiable, Hashable { var dateId: String =
Replies
3
Boosts
0
Views
1.6k
Activity
Aug ’24
Reply to SwiftData Class Inheritance
Check out the talk for SwiftData to get some insight on when you should harness Class Inheritance: https://developer.apple.com/videos/play/wwdc2025/291/ As for the depth and width of your Model Graph, it really depends on the full Schema and how the data is partitioned for the particular use cases.
Replies
Boosts
Views
Activity
Jul ’25
SwiftData: "Illegal attempt to establish a relationship 'item' between objects in different contexts
I have run into this SwiftData issue in multiple projects and have been able to replicate it by building off of the default SwiftData launch project. The original Item class: class Item { var timestamp: Date init(timestamp: Date) { self.timestamp = timestamp } } New MyItem class to replicate the error. Notice I nest an Item object inside MyItem: class MyItem { var name: String var item: Item init(name: String, item: Item) { self.name = name self.item = item } } I then build off of the default view for a SwiftData project. When the '+' button is pressed, a new list item for both Item and MyItem should appear in their appropriate sections. @Environment(.modelContext) private var modelContext @Query private var items: [Item] @Query private var myItems: [MyItem] var body: some View { NavigationSplitView { List { Section(All Items) { ForEach(items) { item in NavigationLink { Text(Item at (item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))) } label: { Text(item.tim
Replies
10
Boosts
0
Views
3.2k
Activity
Oct ’23
Reply to Model instance invalidated, backing data could no longer be found in the store
Update: the problem occurs when the app goes to the background, comes back, and SwiftUI tries to access relationships of SwiftData models that are direct input parameters to the View. My app has a function that opens a URL in a browser, it is when you press the Back button and return from the browser to the app that this fatal error tends to occur. When the app goes to the background, the BackingData of every relationship of this main model seems to get replaced with some subclass of FutureBackingData. Accessing them results in the fatal error above. Explicitly querying the relationship with @Query instead of relying on the relationship property seems to solve the problem. It would be great to have some documentation on how exactly SwiftData is behaving when the app goes to the background.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Dec ’24
Class inheritance with @Observable
Hi there! I'm quite new to SwiftUI; just wanted to know if there any chance to get observation for values in a class that inherits from another class, with the @Observable macro. I have the following class: @Observable class SourceData { var sourceHorizontalResolution: Int? var sourceVerticalResolution: Int? } This class is declared as @State in ContentView, and passed to views using .environment(): struct ContentView: View { @State private var sourceData = SourceData() var body: some View { HStack { VStack { SourceView() .environment(sourceData) //....More code } Finally, I have this class, that would inherit from SourceData: @Observable class PreviewData: SourceData { override init() { super.init() } var placeholderText: NSImage? {...} var previewImage: NSImage? { guard let sourceHorizontalResolution = sourceHorizontalResolution, let sourceVerticalResolution = sourceVerticalResolution else { return placeholderText } // More code here... return image } How can I observe for changes that occ
Replies
0
Boosts
0
Views
760
Activity
Oct ’23
SwiftData Migration Error: Missing Attribute Values on Mandatory Relationship
I'm currently working on a data model migration in Swift using a custom schema migration plan. My project involves migrating settings from one schema version to another (SchemaV1 to SchemaV2). Both schema versions include a class named FSViz, but with slightly different properties. In the newer schema, SchemaV2, I introduced a new property named textSettings of type TextSetting, replacing the textColor property from SchemaV1. Here's a simplified version of my migration code and class definitions: Model: extension SchemaV1 { @Model public final class FSViz { @Attribute(.unique) public let id: UUID public var textColor: TextColor init(textColor: TextColor) { self.id = UUID() self.textColor = textColor } } } extension SchemaV2 { @Model public final class FSViz { @Attribute(.unique) public let id: UUID public var textSettings: TextSetting init(textSettings: TextSetting) { self.id = UUID() self.textSettings = textSettings } } } initMyApp: public struct InitMyApp { static func makeInitialFSViz() -> FSViz? { FSVi
Replies
1
Boosts
0
Views
2.4k
Activity
Feb ’24
Do Generics not inherit initializers?
Four classes, first two normal, Two inherits from One, Two inherits One's initializers as expected.Second two are generic, Four<T> inherits from Three<T>, but everything else is the same. However Four doesn't inherit Three's initializer.Is that a bug or have I missed yet something else in the Swift book?public class One { public var value : Int public init( value : Int ) { self.value = value } } public class Two : One { public func foo() { print( value is (value) ) } } public class Three<T> { public var value : T public init( value : T ) { self.value = value } } public class Four<T> : Three<T> { public func foo() { print( value is (value) ) } } let x = Two( value : 123 ) // *** WORKS ****, Two inherits One's init(value:) method x.foo() let y = Four<Int>( value : 123 ) // Four<Int>' cannot be constructed because it has no accessible initializers y.foo()
Replies
0
Boosts
0
Views
981
Activity
Jun ’15
SwiftData serious bug with relationships and CloudKit in iOS 18.0 (Xcode 16 Beta)
Hi guys. Can someone please confirm this bug so I report it? The issue is that SwiftData relationships don't update the views in some specific situations on devices running iOS 18 Beta. One clear example is with CloudKit. I created a small example for testing. The following code creates two @models, one to store bands and another to store their records. The following code works with no issues. (You need to connect to a CloudKit container and test it on two devices) import SwiftUI import SwiftData struct ContentView: View { @Environment(.modelContext) private var modelContext @Query private var records: [Record] var body: some View { NavigationStack { List(records) { record in VStack(alignment: .leading) { Text(record.title) Text(record.band?.name ?? Undefined) } } .toolbar { ToolbarItem { Button(Add Record) { let randomNumber = Int.random(in: 1...100) let newBand = Band(name: New Band (randomNumber), records: nil) modelContext.insert(newBand) let newRecord = Record(title: New Record
Replies
3
Boosts
0
Views
888
Activity
Sep ’24
Inherit static library into appdlegate
We are trying to integrate an SDK developed in Objective C to iOS app developed in Swift. The SDK’s static library is inherited into App delegate class. Is this an accepted standard per Apple guidelines ? I wanted o check the same to avoid app rejection on submission to store.Eg : AppDelgate.swiftclass AppDelegate : MystaticLibrary,UIApplicationDelegate { //My code }MystaticLibrary-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { //My Code }Thanks in advance!RegardsVignesh B RiOS Developer
Replies
0
Boosts
0
Views
245
Activity
Jan ’16
Resolving App Sandbox Inheritance Problems
This post is part of a cluster of posts related to the trusted execution system. If you found your way here directly, I recommend that you start at the top. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = eskimo + 1 + @ + apple.com Resolving App Sandbox Inheritance Problems If you’re creating a product with the App Sandbox enabled and it crashes with a trap within _libsecinit_appsandbox, it’s likely that you’re tripping over one of the following problems: Nonheritable entitlements Changing sandbox Nothing to inherit Nonheritable Entitlements The most common cause of this problem is also the most obscure. If you have a sandboxed app with an embedded program that you run as a child process, a crash in _libsecinit_appsandbox is most likely caused by the embedded program being signed with entitlements that can’t be inherited. Imagine an, SandboxInit, with an embedded helper tool, NotHeritable. When the app runs the helper tool as a child process
Replies
0
Boosts
0
Views
3.3k
Activity
May ’22
XCode Visual Inheritance
Hello everybody,Just a quick question. Does XCode support visual inheritance? For example if i have an NSWindow which I need to subclass (as it is a common window in the app i need to build), will it's conrols appear on the subclassed one?I've tried several stuff on this, but i could not manage to get it working. Maybe I am missing something. Does anybody have an idea or came across the same case? Any help would be appreciated!
Replies
1
Boosts
0
Views
219
Activity
Aug ’16