Search results for

“SwiftData inheritance relationship”

4,980 results found

Post

Replies

Boosts

Views

Activity

Reply to Error accessing backing data on deleted item in detached task
Yeah, this is an interesting topic. Consider the following flow: In a background task that runs for long time, you fetch a SwiftData model object and hold it for later use. In the main queue, the object is deleted via user interaction. In a background task, you access the object. Based on the current implementation of SwiftData, step 3 will trigger an error because the object was deleted. In the Core Data world, you can use query generations to guarantee the object being valid in step 3, as discussed in Accessing data when the store changes. In the case where, from a managed object context pinned to a generation, you change a piece of data that has been changed, saving the context triggers a conflict, which you can handle via NSMergePolicy. SwiftData doesn't have query generations, and so I don't see an ideal pattern to handle the kind of issue. I may consider the following: a. Avoid accessing a same piece of data simultaneously by shorten the time window. Concretely: In the backgro
Oct ’25
Reply to How to handle required @relationship optionals in SwiftData CloudKit?
Sorry for not being clear. As an example, assuming you have some SwiftUI views that render the relationship, and the views accept only an non-optional array, it will make sense that your SwiftData model creates a computed property to wrap relationship. Here, the Swift views are the other part of your app that prefers to consume a non-optional array. I hope this makes the point clear. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Oct ’25
How to handle required @relationship optionals in SwiftData CloudKit?
Hi all, As you know, when using SwiftData Cloudkit, all relationships are required to be optional. In my app, which is a list app, I have a model class Project that contains an array of Subproject model objects. A Subproject also contains an array of another type of model class and this chain goes on and on. In this type of pattern, it becomes really taxxing to handle the optionals the correct way, i.e. unwrap them as late as possible and display an error to the user if unable to. It seems like most developers don't even bother, they just wrap the array in a computed property that returns an empty array if nil. I'm just wondering what is the recommended way by Apple to handle these optionals. I'm not really familiar with how the CloudKit backend works, but if you have a simple list app that only saves to the users private iCloud, can I just handwave the optionals like so many do? Is it only big data apps that need to worry? Or should we always strive to handle them the correct way? If that's
3
0
199
Oct ’25
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
Reply to Privileged Helper is denied permission to open raw device
Passing along a few notes that may be helpful... First off, yes, this is the block that prevents normal access: At this point, the OS should trigger an interactive dialog, requesting scoped permissions to removable volumes. This works here: First, trigger the OS to intercept a C syscall on the main GUI process: ...because your secondary process is able to inherit its access from the GUI process. Related to this: Manual 'sudo ./helper' call succeeds, however. I suspect this worked because you'd previously granted Terminal.app FDA (Full Disk Access). Without that, you should have either triggered the same volume access dialog or failed (I'm not sure which would have happened). __ Kevin Elliott DTS Engineer, CoreOS/Hardware
Topic: App & System Services SubTopic: Core OS Tags:
Oct ’25
Reply to Privileged Helper is denied permission to open raw device
I think I've finally and by accident achieved a solution. For those who come after... First, trigger the OS to intercept a C syscall on the main GUI process: const fd = c.open(/dev/disk4, c.O_RDWR, @as(c_uint, 0)); // this should return an access denied error, that's OK if (fd < 0) do_nothing() else _ = c.close(fd); At this point, the OS should trigger an interactive dialog, requesting scoped permissions to Removable Volumes. At this point, the Privileged Helper should inherit the permission from the main GUI process and be permitted to write to block device. Repeat C open() syscall procedure on the helper for good measure. Here, however, do not ignore Access Denied error.
Topic: App & System Services SubTopic: Core OS Tags:
Oct ’25
Issue with SwiftData inheritance
Every time I insert a subclass (MYShapeLayer) into the model context, the app crashes with an error: DesignerPlayground crashed due to fatalError in BackingData.swift at line 908. Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0xb2dbc55f3f4c57f2 ))) with Optional(A6CA4F89-107F-4A66-BC49-DD7DAC689F77) struct ContentView: View { @Environment(.modelContext) private var modelContext @Query private var designs: [MYDesign] var layers: [MYLayer] { designs.first?.layers ?? [] } var body: some View { NavigationStack { List { ForEach(layers) { layer in Text(layer.description) } } .onAppear { let design = MYDesign(title: My Design) modelContext.insert(design) try? modelContext.save() } .toolbar { Menu(Add, systemImage: plus) { Button(action: addTextLayer) { Text(Add Text Layer) } Button(action: addShapeLayer) { Text(Add Shape Layer) } } } } } private func addTextLayer() { if let desig
1
0
163
Sep ’25
iOS 26 SwiftData crash does not happen in iOS 16
I have a simple app that makes an HTTPS call to gather some JSON which I then parse and add to my SwiftData database. The app then uses a simple @Query in a view to get the data into a list. on iOS 16 this works fine. No problems. But the same code on iOS 26 (targeting iOS 18.5) crashes after about 15 seconds of idle time after the list is populated. The error message is: Could not cast value of type '__NSCFNumber' (0x1f31ee568) to 'NSString' (0x1f31ec718). and occurs when trying to access ANY property of the list. I have a stripped down version of the app that shows the crash available. To replicate the issue: open the project in Xcode 26 target any iOS 26 device or simulator compile and run the project. after the list is displayed, wait about 15 seconds and the app crashes. It is also of note that if you try to run the app again, it will crash immediately, unless you delete the app from the device. Any help on this would be appreciated. Feedback number FB20295815 includes .zip file Below is the bas
4
0
261
Sep ’25
xcodebuild failing when package plugin is added to project
I have created a build tool plugin in one of my SPM packages, and am trying to get it working in my project. It works fine when I build from Xcode, or have at least built the project in Xcode once before with the plugin. But if I try to build the project using xcodebuild on a machine where I have never built the project before, it fails with this error: error: '2.3.0': Invalid manifest (compiled with: [/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc, -vfsoverlay, /var/folders/5_/q4yl04gs2kld1zztqxkqjdgh0000gq/T/TemporaryDirectory.BWwJWG/vfs.yaml, -L, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI, -lPackageDescription, -Xlinker, -rpath, -Xlinker, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI, -target, arm64-apple-macosx14.0, -sdk, /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.5.sdk, -F, /Applicati
2
0
158
Sep ’25
Reply to Background Assets file diff?
I performed the following code: let assetPackStatus = try await AssetPackManager. Shared. The status (ofAssetPackWithID: assetPackID) print(checkAssetPackStatus (assetPackStatus)). The content of the print assetPackStatus is:(BackgroundAssets. AssetPack. Status) assetPackStatus = (rawValue = 69). I couldn't find the explanation for this content in AssetPack.Status. Could you please tell me what it means. Thanks. AssetPack.Status is an option set, not an enumeration. One status value can include zero or more options. Its raw value is a mask on a bit field of the possible options. Each possible option is represented by a 1 bit at a particular location in the bit field—i.e., the binary representation of some power of 2. The bit mask selects some subset of those options. It’s just a sequence of bits, so it can be typed as an integer. In your case, the integer 69 in binary is 0b1000101. The three 1 bits, from right to left (least significant to most significant), correspond to 2⁰, 2², and 2⁶, respectively. Here’s
Sep ’25
swift
Hi, thank you for your reply. I have checked and confirmed that all AppleUser entity fields (id, name, email, password, createdAt) are optional, relationships (posts, comments) are optional, and I assign values when creating a new object, but Core Data still throws a nilError during registration; I have uploaded my project to GitHub for your reference here: https://github.com/Kawiichao/job. If reviewing it requires any payment, please let me know in advance. Thank you very much for your kind offer—I really appreciate it!
1
0
68
Sep ’25
Reply to @Attribute 'unique' and complex keys
This is a very nice addition to SwiftData, but there is still a problem (which I didn't mention in the original post). To the best of my knowledge, CloudKit still does not support (or enforce) unique properties. So, unless I'm missing something, this solution only works if you don't want take advantage of iCloud data synchronization services. Or am I missing something?
Sep ’25
@Attribute 'unique' and complex keys
The 'unique' attribute is a really nice feature, BUT. In some of my apps, the unique identifier for an object is a combination of multiple attributes. (Example: a book title is not unique, but a combination of book title and author list is.) How do I model this with SwiftData? I cannot use @Attribute(.unique) on either the title OR the author list, but I want SwiftData to provide the same insert or update logic. Is this possible?
5
0
3.0k
Sep ’25
Reply to Error accessing backing data on deleted item in detached task
Yeah, this is an interesting topic. Consider the following flow: In a background task that runs for long time, you fetch a SwiftData model object and hold it for later use. In the main queue, the object is deleted via user interaction. In a background task, you access the object. Based on the current implementation of SwiftData, step 3 will trigger an error because the object was deleted. In the Core Data world, you can use query generations to guarantee the object being valid in step 3, as discussed in Accessing data when the store changes. In the case where, from a managed object context pinned to a generation, you change a piece of data that has been changed, saving the context triggers a conflict, which you can handle via NSMergePolicy. SwiftData doesn't have query generations, and so I don't see an ideal pattern to handle the kind of issue. I may consider the following: a. Avoid accessing a same piece of data simultaneously by shorten the time window. Concretely: In the backgro
Replies
Boosts
Views
Activity
Oct ’25
Using SwiftData in background?
How does SwiftData work with background operations? CoreData had background context that could be used to avoid UI hang for heavy operations. Is there an equivalent in SwiftData, and if so, do I have to merge changes or does it save directly to persistent store?
Replies
5
Boosts
0
Views
5.6k
Activity
Oct ’25
Reply to How to handle required @relationship optionals in SwiftData CloudKit?
Sorry for not being clear. As an example, assuming you have some SwiftUI views that render the relationship, and the views accept only an non-optional array, it will make sense that your SwiftData model creates a computed property to wrap relationship. Here, the Swift views are the other part of your app that prefers to consume a non-optional array. I hope this makes the point clear. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Oct ’25
How to handle required @relationship optionals in SwiftData CloudKit?
Hi all, As you know, when using SwiftData Cloudkit, all relationships are required to be optional. In my app, which is a list app, I have a model class Project that contains an array of Subproject model objects. A Subproject also contains an array of another type of model class and this chain goes on and on. In this type of pattern, it becomes really taxxing to handle the optionals the correct way, i.e. unwrap them as late as possible and display an error to the user if unable to. It seems like most developers don't even bother, they just wrap the array in a computed property that returns an empty array if nil. I'm just wondering what is the recommended way by Apple to handle these optionals. I'm not really familiar with how the CloudKit backend works, but if you have a simple list app that only saves to the users private iCloud, can I just handwave the optionals like so many do? Is it only big data apps that need to worry? Or should we always strive to handle them the correct way? If that's
Replies
3
Boosts
0
Views
199
Activity
Oct ’25
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
Reply to Privileged Helper is denied permission to open raw device
Passing along a few notes that may be helpful... First off, yes, this is the block that prevents normal access: At this point, the OS should trigger an interactive dialog, requesting scoped permissions to removable volumes. This works here: First, trigger the OS to intercept a C syscall on the main GUI process: ...because your secondary process is able to inherit its access from the GUI process. Related to this: Manual 'sudo ./helper' call succeeds, however. I suspect this worked because you'd previously granted Terminal.app FDA (Full Disk Access). Without that, you should have either triggered the same volume access dialog or failed (I'm not sure which would have happened). __ Kevin Elliott DTS Engineer, CoreOS/Hardware
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Oct ’25
Reply to Privileged Helper is denied permission to open raw device
I think I've finally and by accident achieved a solution. For those who come after... First, trigger the OS to intercept a C syscall on the main GUI process: const fd = c.open(/dev/disk4, c.O_RDWR, @as(c_uint, 0)); // this should return an access denied error, that's OK if (fd < 0) do_nothing() else _ = c.close(fd); At this point, the OS should trigger an interactive dialog, requesting scoped permissions to Removable Volumes. At this point, the Privileged Helper should inherit the permission from the main GUI process and be permitted to write to block device. Repeat C open() syscall procedure on the helper for good measure. Here, however, do not ignore Access Denied error.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Oct ’25
Issue with SwiftData inheritance
Every time I insert a subclass (MYShapeLayer) into the model context, the app crashes with an error: DesignerPlayground crashed due to fatalError in BackingData.swift at line 908. Never access a full future backing data - PersistentIdentifier(id: SwiftData.PersistentIdentifier.ID(backing: SwiftData.PersistentIdentifier.PersistentIdentifierBacking.managedObjectID(0xb2dbc55f3f4c57f2 ))) with Optional(A6CA4F89-107F-4A66-BC49-DD7DAC689F77) struct ContentView: View { @Environment(.modelContext) private var modelContext @Query private var designs: [MYDesign] var layers: [MYLayer] { designs.first?.layers ?? [] } var body: some View { NavigationStack { List { ForEach(layers) { layer in Text(layer.description) } } .onAppear { let design = MYDesign(title: My Design) modelContext.insert(design) try? modelContext.save() } .toolbar { Menu(Add, systemImage: plus) { Button(action: addTextLayer) { Text(Add Text Layer) } Button(action: addShapeLayer) { Text(Add Shape Layer) } } } } } private func addTextLayer() { if let desig
Replies
1
Boosts
0
Views
163
Activity
Sep ’25
iOS 26 SwiftData crash does not happen in iOS 16
I have a simple app that makes an HTTPS call to gather some JSON which I then parse and add to my SwiftData database. The app then uses a simple @Query in a view to get the data into a list. on iOS 16 this works fine. No problems. But the same code on iOS 26 (targeting iOS 18.5) crashes after about 15 seconds of idle time after the list is populated. The error message is: Could not cast value of type '__NSCFNumber' (0x1f31ee568) to 'NSString' (0x1f31ec718). and occurs when trying to access ANY property of the list. I have a stripped down version of the app that shows the crash available. To replicate the issue: open the project in Xcode 26 target any iOS 26 device or simulator compile and run the project. after the list is displayed, wait about 15 seconds and the app crashes. It is also of note that if you try to run the app again, it will crash immediately, unless you delete the app from the device. Any help on this would be appreciated. Feedback number FB20295815 includes .zip file Below is the bas
Replies
4
Boosts
0
Views
261
Activity
Sep ’25
xcodebuild failing when package plugin is added to project
I have created a build tool plugin in one of my SPM packages, and am trying to get it working in my project. It works fine when I build from Xcode, or have at least built the project in Xcode once before with the plugin. But if I try to build the project using xcodebuild on a machine where I have never built the project before, it fails with this error: error: '2.3.0': Invalid manifest (compiled with: [/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc, -vfsoverlay, /var/folders/5_/q4yl04gs2kld1zztqxkqjdgh0000gq/T/TemporaryDirectory.BWwJWG/vfs.yaml, -L, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI, -lPackageDescription, -Xlinker, -rpath, -Xlinker, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI, -target, arm64-apple-macosx14.0, -sdk, /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX15.5.sdk, -F, /Applicati
Replies
2
Boosts
0
Views
158
Activity
Sep ’25
Reply to Background Assets file diff?
I performed the following code: let assetPackStatus = try await AssetPackManager. Shared. The status (ofAssetPackWithID: assetPackID) print(checkAssetPackStatus (assetPackStatus)). The content of the print assetPackStatus is:(BackgroundAssets. AssetPack. Status) assetPackStatus = (rawValue = 69). I couldn't find the explanation for this content in AssetPack.Status. Could you please tell me what it means. Thanks. AssetPack.Status is an option set, not an enumeration. One status value can include zero or more options. Its raw value is a mask on a bit field of the possible options. Each possible option is represented by a 1 bit at a particular location in the bit field—i.e., the binary representation of some power of 2. The bit mask selects some subset of those options. It’s just a sequence of bits, so it can be typed as an integer. In your case, the integer 69 in binary is 0b1000101. The three 1 bits, from right to left (least significant to most significant), correspond to 2⁰, 2², and 2⁶, respectively. Here’s
Replies
Boosts
Views
Activity
Sep ’25
Reply to BottomBar .searchable + Tab with Nav
The developer tutorial exhibits this issue nicely: https://developer.apple.com/tutorials/develop-in-swift/work-with-relationships
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Sep ’25
swift
Hi, thank you for your reply. I have checked and confirmed that all AppleUser entity fields (id, name, email, password, createdAt) are optional, relationships (posts, comments) are optional, and I assign values when creating a new object, but Core Data still throws a nilError during registration; I have uploaded my project to GitHub for your reference here: https://github.com/Kawiichao/job. If reviewing it requires any payment, please let me know in advance. Thank you very much for your kind offer—I really appreciate it!
Replies
1
Boosts
0
Views
68
Activity
Sep ’25
Reply to @Attribute 'unique' and complex keys
This is a very nice addition to SwiftData, but there is still a problem (which I didn't mention in the original post). To the best of my knowledge, CloudKit still does not support (or enforce) unique properties. So, unless I'm missing something, this solution only works if you don't want take advantage of iCloud data synchronization services. Or am I missing something?
Replies
Boosts
Views
Activity
Sep ’25
@Attribute 'unique' and complex keys
The 'unique' attribute is a really nice feature, BUT. In some of my apps, the unique identifier for an object is a combination of multiple attributes. (Example: a book title is not unique, but a combination of book title and author list is.) How do I model this with SwiftData? I cannot use @Attribute(.unique) on either the title OR the author list, but I want SwiftData to provide the same insert or update logic. Is this possible?
Replies
5
Boosts
0
Views
3.0k
Activity
Sep ’25