Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

Migrating a swiftData project to CloudKit to implement iCloudSync.
My project is using swiftData and I want to implement iCloud sync in it. Now, my data base doesnt have any optional attributes or relationships and CloudKit wants them to be optional. So, rather than editing all code with unwrapping code for the optionals, how can I provide a bridge that does so in the last stage of actually saving to the store? Sort of, capture it in a proxy object before writing and after reading from the store. Is there a neat way that can save a lot of debugging? I have code snippets from chat gpt and they are hard to debug. This is my first project in swiftUI. Thanks. Neerav
3
0
190
Jun ’25
SwiftData crash on fetch
I have a strange crash which I have problems understanding. It only happens on a few devices, after a ModelContainer migration, and it doesn't seem to crash on the migration itself. The fetch is done in onAppear, and shouldn't necessarily result in a crash, as it is an optional try: let request = FetchDescriptor() let data = try? modelContext.fetch(request) if let data, !data.isEmpty { rifle = data.first(where: { $0.uuid.uuidString == settings.selectedRifleId }) ?? data.first! } When I get logs from users, there seems to be an error in encoding? Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x000000018e8bfd78 Termination Reason: SIGNAL 5 Trace/BPT trap: 5 Terminating Process: exc handler [71687] Triggered by Thread: 0 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0 Crashed: 0 libswiftCore.dylib 0x18e8bfd78 _assertionFailure(_:_:file:line:flags:) + 264 1 SwiftData 0x24e18b480 0x24e14c000 + 259200 2 SwiftData 0x24e193968 0x24e14c000 + 293224 3 SwiftData
9
0
862
Jun ’25
SwiftData iOS18: "Could not materialize Objective-C class named "Set" from declared attribute value type "Set<String>" of attribute named..."
Posting here to see if folks have workarounds or if I have a misunderstanding of SwiftData supported types. In adopting SwiftData, I have swiftData properties of collection type (Array or Set - both have this issue). E.g: @Model final class Item { var timestamp: Date var strings = [aa, bb] var display: String { strings.joined(separator: ) } init(timestamp: Date) { self.timestamp = timestamp } } So far in development I haven't had issues on iOS 17, but on the iOS 18 betas 4-5 the app logs show the following error: fault: Could not materialize Objective-C class named Array from declared attribute value type Array of attribute named strings It happens immediately in my app when creating an object with a collection attribute. In a minimal test example, the error log appears only after a few minutes and doesn't seem to affect the template app's basic functionality. Anyone else running into this? Was filed as FB14397250
8
0
2.7k
Apr ’25
CATiledLayer flashes and re-draws entirely when re-drawing a single tile
I have filed a bug report for this (FB17734946), but I'm posting it here verbatim in case others have the same issue and in hopes of getting attention from an Apple engineer sooner. When calling setNeedsDisplayInRect on a CATiledLayer - or a UIView whose backing layer is CATiledLayer - one would expect to re-draw only a region identified by the rect passed to the method. This is even written in the documentation for the class: Regions of the layer may be invalidated using the setNeedsDisplayInRect: method however the update will be asynchronous. While the next display update will most likely not contain the updated content, a future update will. However, upon calling this method, CATiledLayer redraws whole contents instead of just the tile at the specified rect, and it flashes when doing so. It behaves exactly the same as if one had called setNeedsDisplay without passing any rect; all contents are cleared and re-drawn again. I'm 100% sure I've passed in the correct rect of the exact tile that I need to redraw
3
0
204
May ’25
SwiftData updates delay in widget
My SwiftData model doesn't have time to update, and the widget gets old data on the timeline I create simple SwiftData app with widget extension. To share swiftdata between widget and app I use the same approach as in sample code Adopting SwiftData for a Core Data app: let modelContext = ModelContext(DataModel.shared.modelContainer) if let models = try? modelContext.fetch(fetchDescriptor) { // use } After I create/delete/updated model I call: WidgetCenter.shared.reloadAllTimelines() The problem is that the widget receives outdated data as a result of modelContext.fetch. To get the new data I need to call WidgetCenter.shared.reloadAllTimelines() twice or with some delay: // MyModelEditorView private func save() { if let myModel { myModel.title = title } else { let myModel = MyModel( title: title ) modelContext.insert(word) } WidgetCenter.shared.reloadAllTimelines() // i have to call it twice WidgetCenter.shared.reloadAllTimelines() } I couldn't test SampleCode for similar up
2
0
778
Jul ’24
Reply to No persistent stores error in SwiftData
The crash you’re seeing— FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation. (Apple Developer) —happens because by the time your onAppear runs, the debug block has already loaded and then removed the only store from the underlying NSPersistentCloudKitContainer. When you immediately call newContainer = try ModelContainer(for: Page.self, configurations: config) SwiftData’s internal coordinator finds no stores to save into and throws. The fix is to perform both the CloudKit‐schema initialization and your ModelContainer setup early, in your App struct (or in its init), rather than inside an onAppear. That way SwiftData creates its SQLite store after you’ve torn down the temporary CloudKit container, so it can recreate a fresh store. For example: @main struct MyApp: App { // 1) Build your debug store, initializeCloudKitSchema, remove it… // 2) Then immediately create the SwiftData ModelContaine
May ’25
Mixing ReferenceFileDocument and @Observable
I have an app in which the data model is @Observable, and views see it through @Environment(dataModel.self) private var dataModel. Since there are a large number of views, only some of which may need to be redrawn at a given time, I believe that @Observable is more efficient at run time than @Published and @ObservedObject I’ve been trying to make the app document based. Although I started using SwiftData, it has trouble with Codable, and a long thread in the Developer forum suggests that SwiftData does not support the Undo manager - and in any event, simple JSON serialization is all that this app requires. Unfortunately, ReferenceFileDocument inherits from ObservableObject, which seems to not play nice with @Observable. I’d like to keep using @Observable, but haven’t been able to figure out how. When I deserialize a JSON ReferenceFileDocument, I can’t seem to connect it to an @Observable class instance and to let the various views and view models know where to find and update it. I’
4
0
170
May ’25
Reply to Mixing ReferenceFileDocument and @Observable
When I register Undos, I name them so that I can make the undo available via a menu selection that shows specifically what will be undone or redone. Hoping that a shake gesture will work seems much less desirable than giving the user explicit control over undo / redo - and in any event, I've read that the Undo manager doesn't actually work with SwiftData.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Does adding a bar button item to the navigation bar programmatically when offering scene support differ from the old app delegate approach
Ok, now I understand. In my original project, I mistakenly made my root view controller be the initial controller when I was upgrading to a scene lifecycle. I did have a navigation controller, but when I made my root view controller the initial controller, it removed the initial controller status from the navigation controller. That prevented the navigation bar item from working. So in this test project, once I added a navigation controller (as you suggested) and made the ViewController have a root view controller relationship to the navigation controller, then it works. Thanks.
Topic: UI Frameworks SubTopic: UIKit Tags:
May ’25
No persistent stores error in SwiftData
I am following Apple's instruction to sync SwiftData with CloudKit. While initiating the ModelContainer, right after removing the store from Core Data, the error occurs: FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.; (user info absent) I've tried removing default.store and its related files/folders before creating the ModelContainer with FileManager but it does not resolve the issue. Isn't it supposed to create a new store when the ModelContainer is initialized? I don't understand why this error occurs. Error disappears when I comment out the #if DEBUG block. Code: import CoreData import SwiftData import SwiftUI struct InitView: View { @Binding var modelContainer: ModelContainer? @Binding var isReady: Bool @State private var loadingDots = @State private var timer: Timer? var body: some View { VStack(spacing: 16) { Text(Loading(loadingDots)) .font(.title2) .foregroundColor(.gray) } .padding()
2
0
197
May ’25
When I inherit from a class assigning SceneLocationView() sceneNode is nil
When I try to post a LocationAnnotations of a subclass respect to the one defining variable sceneLocationView, by way of: sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: bannerAnnotationLocation) When I enter in: public func addLocationNodeWithConfirmedLocation(locationNode: LocationNode) { if locationNode.location == nil || locationNode.locationConfirmed == false { return } updatePositionAndScaleOfLocationNode(locationNode: locationNode, initialSetup: true, animated: true) locationNodes.append(locationNode) print(sceneNode?.description ?? nil) self.sceneNode?.addChildNode(locationNode) } I find sceneNode to nil, like if the effect of the renderer is lost when subclassing. I also tried to create a new variable in the subclass also assigning SceneLocationView(), but sceneNode remains nil. What to do to force it to assume a value?
1
0
75
May ’25
Reply to Mixing ReferenceFileDocument and @Observable
Unfortunately, ReferenceFileDocument inherits from ObservableObject, which seems to not play nice with @Observable. Is there any specific reason to make the document type a class, you could have a FileDocument-conforming struct and vent that to your View as a State or through a model object that conforms to @Observable.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to How to distribute DEXT during development and to the public
With regard to this issue, I confirmed that this error occurs when the bundle ID length is longer than 64 characters. Are there any such limitation? Yes. More specifically, because of how DEXTs are integrated into the kernel, they end up inheriting limitation/restrictions that the kernel implements but the larger system does not. As another example, many Xcode project use CFBundleVersion for an incrementing build number but the kernel actually enforces it's own fairly rigid format which the kextload system then enforces on DEXTs. __ Kevin Elliott DTS Engineer, CoreOS/Hardware
May ’25
Reply to iOS Camera access issues in Developer mode on real device - PermissionStatus.permanentlyDenied
Looks like I had to modify iOS/Podfile to make this work # --- BEGIN: permission_handler camera macro --- # The following block explicitly enables camera permission for the permission_handler plugin. # Remove or comment out this block if you want to revert to default behavior. post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)', 'PERMISSION_CAMERA=1', # <-- Explicitly enable camera permission ] end end end # --- END: permission_handler camera macro ---
May ’25
Migrating a swiftData project to CloudKit to implement iCloudSync.
My project is using swiftData and I want to implement iCloud sync in it. Now, my data base doesnt have any optional attributes or relationships and CloudKit wants them to be optional. So, rather than editing all code with unwrapping code for the optionals, how can I provide a bridge that does so in the last stage of actually saving to the store? Sort of, capture it in a proxy object before writing and after reading from the store. Is there a neat way that can save a lot of debugging? I have code snippets from chat gpt and they are hard to debug. This is my first project in swiftUI. Thanks. Neerav
Replies
3
Boosts
0
Views
190
Activity
Jun ’25
Reply to Migrating a swiftData project to CloudKit to implement iCloudSync.
What do I do with relationships? class Category { var expenses: [Expense] = [] }
Replies
Boosts
Views
Activity
Jun ’25
SwiftData crash on fetch
I have a strange crash which I have problems understanding. It only happens on a few devices, after a ModelContainer migration, and it doesn't seem to crash on the migration itself. The fetch is done in onAppear, and shouldn't necessarily result in a crash, as it is an optional try: let request = FetchDescriptor() let data = try? modelContext.fetch(request) if let data, !data.isEmpty { rifle = data.first(where: { $0.uuid.uuidString == settings.selectedRifleId }) ?? data.first! } When I get logs from users, there seems to be an error in encoding? Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x000000018e8bfd78 Termination Reason: SIGNAL 5 Trace/BPT trap: 5 Terminating Process: exc handler [71687] Triggered by Thread: 0 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0 Crashed: 0 libswiftCore.dylib 0x18e8bfd78 _assertionFailure(_:_:file:line:flags:) + 264 1 SwiftData 0x24e18b480 0x24e14c000 + 259200 2 SwiftData 0x24e193968 0x24e14c000 + 293224 3 SwiftData
Replies
9
Boosts
0
Views
862
Activity
Jun ’25
SwiftData iOS18: "Could not materialize Objective-C class named "Set" from declared attribute value type "Set<String>" of attribute named..."
Posting here to see if folks have workarounds or if I have a misunderstanding of SwiftData supported types. In adopting SwiftData, I have swiftData properties of collection type (Array or Set - both have this issue). E.g: @Model final class Item { var timestamp: Date var strings = [aa, bb] var display: String { strings.joined(separator: ) } init(timestamp: Date) { self.timestamp = timestamp } } So far in development I haven't had issues on iOS 17, but on the iOS 18 betas 4-5 the app logs show the following error: fault: Could not materialize Objective-C class named Array from declared attribute value type Array of attribute named strings It happens immediately in my app when creating an object with a collection attribute. In a minimal test example, the error log appears only after a few minutes and doesn't seem to affect the template app's basic functionality. Anyone else running into this? Was filed as FB14397250
Replies
8
Boosts
0
Views
2.7k
Activity
Apr ’25
CATiledLayer flashes and re-draws entirely when re-drawing a single tile
I have filed a bug report for this (FB17734946), but I'm posting it here verbatim in case others have the same issue and in hopes of getting attention from an Apple engineer sooner. When calling setNeedsDisplayInRect on a CATiledLayer - or a UIView whose backing layer is CATiledLayer - one would expect to re-draw only a region identified by the rect passed to the method. This is even written in the documentation for the class: Regions of the layer may be invalidated using the setNeedsDisplayInRect: method however the update will be asynchronous. While the next display update will most likely not contain the updated content, a future update will. However, upon calling this method, CATiledLayer redraws whole contents instead of just the tile at the specified rect, and it flashes when doing so. It behaves exactly the same as if one had called setNeedsDisplay without passing any rect; all contents are cleared and re-drawn again. I'm 100% sure I've passed in the correct rect of the exact tile that I need to redraw
Replies
3
Boosts
0
Views
204
Activity
May ’25
SwiftData updates delay in widget
My SwiftData model doesn't have time to update, and the widget gets old data on the timeline I create simple SwiftData app with widget extension. To share swiftdata between widget and app I use the same approach as in sample code Adopting SwiftData for a Core Data app: let modelContext = ModelContext(DataModel.shared.modelContainer) if let models = try? modelContext.fetch(fetchDescriptor) { // use } After I create/delete/updated model I call: WidgetCenter.shared.reloadAllTimelines() The problem is that the widget receives outdated data as a result of modelContext.fetch. To get the new data I need to call WidgetCenter.shared.reloadAllTimelines() twice or with some delay: // MyModelEditorView private func save() { if let myModel { myModel.title = title } else { let myModel = MyModel( title: title ) modelContext.insert(word) } WidgetCenter.shared.reloadAllTimelines() // i have to call it twice WidgetCenter.shared.reloadAllTimelines() } I couldn't test SampleCode for similar up
Replies
2
Boosts
0
Views
778
Activity
Jul ’24
Reply to No persistent stores error in SwiftData
The crash you’re seeing— FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation. (Apple Developer) —happens because by the time your onAppear runs, the debug block has already loaded and then removed the only store from the underlying NSPersistentCloudKitContainer. When you immediately call newContainer = try ModelContainer(for: Page.self, configurations: config) SwiftData’s internal coordinator finds no stores to save into and throws. The fix is to perform both the CloudKit‐schema initialization and your ModelContainer setup early, in your App struct (or in its init), rather than inside an onAppear. That way SwiftData creates its SQLite store after you’ve torn down the temporary CloudKit container, so it can recreate a fresh store. For example: @main struct MyApp: App { // 1) Build your debug store, initializeCloudKitSchema, remove it… // 2) Then immediately create the SwiftData ModelContaine
Replies
Boosts
Views
Activity
May ’25
Mixing ReferenceFileDocument and @Observable
I have an app in which the data model is @Observable, and views see it through @Environment(dataModel.self) private var dataModel. Since there are a large number of views, only some of which may need to be redrawn at a given time, I believe that @Observable is more efficient at run time than @Published and @ObservedObject I’ve been trying to make the app document based. Although I started using SwiftData, it has trouble with Codable, and a long thread in the Developer forum suggests that SwiftData does not support the Undo manager - and in any event, simple JSON serialization is all that this app requires. Unfortunately, ReferenceFileDocument inherits from ObservableObject, which seems to not play nice with @Observable. I’d like to keep using @Observable, but haven’t been able to figure out how. When I deserialize a JSON ReferenceFileDocument, I can’t seem to connect it to an @Observable class instance and to let the various views and view models know where to find and update it. I’
Replies
4
Boosts
0
Views
170
Activity
May ’25
Reply to Mixing ReferenceFileDocument and @Observable
When I register Undos, I name them so that I can make the undo available via a menu selection that shows specifically what will be undone or redone. Hoping that a shake gesture will work seems much less desirable than giving the user explicit control over undo / redo - and in any event, I've read that the Undo manager doesn't actually work with SwiftData.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Does adding a bar button item to the navigation bar programmatically when offering scene support differ from the old app delegate approach
Ok, now I understand. In my original project, I mistakenly made my root view controller be the initial controller when I was upgrading to a scene lifecycle. I did have a navigation controller, but when I made my root view controller the initial controller, it removed the initial controller status from the navigation controller. That prevented the navigation bar item from working. So in this test project, once I added a navigation controller (as you suggested) and made the ViewController have a root view controller relationship to the navigation controller, then it works. Thanks.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
May ’25
No persistent stores error in SwiftData
I am following Apple's instruction to sync SwiftData with CloudKit. While initiating the ModelContainer, right after removing the store from Core Data, the error occurs: FAULT: NSInternalInconsistencyException: This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation.; (user info absent) I've tried removing default.store and its related files/folders before creating the ModelContainer with FileManager but it does not resolve the issue. Isn't it supposed to create a new store when the ModelContainer is initialized? I don't understand why this error occurs. Error disappears when I comment out the #if DEBUG block. Code: import CoreData import SwiftData import SwiftUI struct InitView: View { @Binding var modelContainer: ModelContainer? @Binding var isReady: Bool @State private var loadingDots = @State private var timer: Timer? var body: some View { VStack(spacing: 16) { Text(Loading(loadingDots)) .font(.title2) .foregroundColor(.gray) } .padding()
Replies
2
Boosts
0
Views
197
Activity
May ’25
When I inherit from a class assigning SceneLocationView() sceneNode is nil
When I try to post a LocationAnnotations of a subclass respect to the one defining variable sceneLocationView, by way of: sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: bannerAnnotationLocation) When I enter in: public func addLocationNodeWithConfirmedLocation(locationNode: LocationNode) { if locationNode.location == nil || locationNode.locationConfirmed == false { return } updatePositionAndScaleOfLocationNode(locationNode: locationNode, initialSetup: true, animated: true) locationNodes.append(locationNode) print(sceneNode?.description ?? nil) self.sceneNode?.addChildNode(locationNode) } I find sceneNode to nil, like if the effect of the renderer is lost when subclassing. I also tried to create a new variable in the subclass also assigning SceneLocationView(), but sceneNode remains nil. What to do to force it to assume a value?
Replies
1
Boosts
0
Views
75
Activity
May ’25
Reply to Mixing ReferenceFileDocument and @Observable
Unfortunately, ReferenceFileDocument inherits from ObservableObject, which seems to not play nice with @Observable. Is there any specific reason to make the document type a class, you could have a FileDocument-conforming struct and vent that to your View as a State or through a model object that conforms to @Observable.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to How to distribute DEXT during development and to the public
With regard to this issue, I confirmed that this error occurs when the bundle ID length is longer than 64 characters. Are there any such limitation? Yes. More specifically, because of how DEXTs are integrated into the kernel, they end up inheriting limitation/restrictions that the kernel implements but the larger system does not. As another example, many Xcode project use CFBundleVersion for an incrementing build number but the kernel actually enforces it's own fairly rigid format which the kextload system then enforces on DEXTs. __ Kevin Elliott DTS Engineer, CoreOS/Hardware
Replies
Boosts
Views
Activity
May ’25
Reply to iOS Camera access issues in Developer mode on real device - PermissionStatus.permanentlyDenied
Looks like I had to modify iOS/Podfile to make this work # --- BEGIN: permission_handler camera macro --- # The following block explicitly enables camera permission for the permission_handler plugin. # Remove or comment out this block if you want to revert to default behavior. post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) target.build_configurations.each do |config| config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)', 'PERMISSION_CAMERA=1', # <-- Explicitly enable camera permission ] end end end # --- END: permission_handler camera macro ---
Replies
Boosts
Views
Activity
May ’25