Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

Reply to Error when running a modelContext rollback
Hi - I am getting the same error, but on modelContext.delete(). I went through checking all the relationship ids, and they are all fine. All models seem to be registered and can be accessed through relationships, fetches or @Query. . I have had some success with SwiftData, using in a semi-production app for about a year. I have never seen this error message before. This seems like a new bug to me.
May ’25
Document-based SwiftData apps fail to identify a store on iPad?
When trying to run my document-based iPad app using iPadOS 18 beta and Xcode 16 beta, I get an error like the following after opening a document: Thread 1: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData from [:] In order to help track down what is going wrong, I downloaded the sample app from WWDC23 session Build an app with SwiftData found here: https://developer.apple.com/documentation/swiftui/building-a-document-based-app-using-swiftdata When I try to run the end-state of that sample code, I get a similar error when running the app on my iPad and creating a new deck: Thread 1: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData from [:] Given that the sample project is generating the same error as my own project, is this a problem with SwiftData and document-based apps in general? Or is there a change of approach that I should try?
19
0
2.9k
Feb ’25
SwiftData updates in the background are not merged in the main UI context
Hello, SwiftData is not working correctly with Swift Concurrency. And it’s sad after all this time. I personally found a regression. The attached code works perfectly fine on iOS 17.5 but doesn’t work correctly on iOS 18 or iOS 18.1. A model can be updated from the background (Task, Task.detached or ModelActor) and refreshes the UI, but as soon as the same item is updated from the View (fetched via a Query), the next background updates are not reflected anymore in the UI, the UI is not refreshed, the updates are not merged into the main. How to reproduce: Launch the app Tap the plus button in the navigation bar to create a new item Tap on the “Update from Task”, “Update from Detached Task”, “Update from ModelActor” many times Notice the time is updated Tap on the “Update from View” (once or many times) Notice the time is updated Tap again on “Update from Task”, “Update from Detached Task”, “Update from ModelActor” many times Notice that the time is not update anymore Am I doing something wrong? Or is
1
0
834
Apr ’25
How to import large data from Server and save it to Swift Data
Here’s the situation: • You’re downloading a huge list of data from iCloud. • You’re saving it one by one (sequentially) into SwiftData. • You don’t want the SwiftUI view to refresh until all the data is imported. • After all the import is finished, SwiftUI should show the new data. The Problem If you insert into the same ModelContext that SwiftUI’s @Environment(.modelContext) is watching, each insert may cause SwiftUI to start reloading immediately. That will make the UI feel slow, and glitchy, because SwiftUI will keep trying to re-render while you’re still importing. How to achieve this in Swift Data ?
2
0
147
Apr ’25
Reply to How to import large data from Server and save it to Swift Data
One way would be to perform the import in the background in a @ModelActor. Something like this: @ModelActor actor ImportService { func import(data: [Data]) throws { for date in data { let model = Model(/* ... */) modelContext.insert(model) } try modelContext.save() } } This way the import will not block the UI, and the imported data will only be visible in the UI after modelContext.save() is called. I haven't used SwiftData in a real-world app yet, however, so do be careful. For example I don't know if SwiftData will keep the models in memory until save() is called, so you might have to save more frequently. But even then, the UI will not update for every newly inserted model but only when you choose.
Apr ’25
Reply to iOS 16.3 in Simulator
Definitely an issue in xCode 16.3 as it applies to inherited properties and the simulators. I can't diagnose why it's happening, but there are cases where the value shown in the debugger is not what it should be and that causes code to fail. What's crazy is that looking in the debugger at self.inheritedProperty and then expanding self and drilling down to inheritedProperty, the values do not match. The value on the self node is correct. The issue does not occur on a real device and Xcode 16.0 is still solid as it applies to simulators. Personally, I will be avoiding xCode 16.3 until Apple issues an update that addresses a fix for this.
Apr ’25
Reply to How to import large data from Server and save it to Swift Data
Do you know how much data is expected to be downloaded? If so, you could ensure the view doesn't update until the right amount of data is received and stored. If you don't know the quantity of data, you could add a Bool and toggle it when the data loading starts, then toggle it when the data has finished loading, so the View is only refreshed when the last of the data is stored. Another way would be to disable the UI so the user can't interact with it until the data has finished loading. The UI would update in the background, but the user wouldn't experience a slow UI as they can't interact with it anyway. You could put a partially-transparent View with a ProgressView in it at the top of a ZStack (which would be at the bottom of the ZStack in the code...) and hide it when you're done. I'll show you how I do it below. There's a few ways of doing it, but I don't think there's a built-in way to achieve this. But then, I'm not a genius on SwiftData, so... Using a blocking view: @State private var loading
Apr ’25
Question about BGAppRefreshTask approach for medication scheduling app
I'm developing a medication scheduling app similar to Apple Health's Medications feature, and I'd like some input on my current approach to background tasks. In my app, when a user creates a medication, I generate ScheduledDose objects (with corresponding local notifications) for the next 2 weeks and save them to SwiftData. To ensure this 2-week window stays current, I've implemented a BGAppRefreshTask that runs daily to generate new doses as needed. My concern is whether BGAppRefreshTask is the appropriate mechanism for this purpose. Since I'm not making any network requests but rather generating and storing local data, I'm questioning if this is the right approach. I'm also wondering how Apple Health's Medications feature handles this kind of scheduling. Their app seems to maintain future doses regardless of app usage patterns. Has anyone implemented something similar or can suggest the best background execution API for this type of scenario? Thanks for any guidance you can provide.
2
0
193
Apr ’25
SwiftData rollback not updating the UI
I'm building a simple App using SwiftData. In my app a user can create, remove and edit posts. When editing, I want them to be able to hit Save to persist the changes or Cancel to discard the changes. The approach I'm using is to disable autosave and call modelContext.save() when saving and modelContext.rollback() when discarding the changes. So my modelContainer is defined as follows: WindowGroup { ContentView() .modelContainer(for: [Post.self], isAutosaveEnabled: false) } and I Save and Cancel like this: PostForm(post: post) .toolbar { ToolbarItemGroup(placement: .cancellationAction) { Button(Cancel) { if modelContext.hasChanges { modelContext.rollback() } dismiss() } } ToolbarItemGroup(placement: .confirmationAction) { Button(Save) { do { if modelContext.hasChanges { try modelContext.save() } } catch { fatalError(Failed to save post: (error.localizedDescription)) } callback?() dismiss() } } } The issue I am facing is that after calling modelContext.rollback() my Posts aren't updating in the UI, th
2
0
565
Jul ’24
Ongoing Issues with ModelActor in SwiftData?
After the problems with the ModelActor in iOS 18, it seemed like the ModelActor became more stable with iOS 18.1 and macOS 15.1. However, I’m still encountering many problems and crashes. I wanted to ask if these issues are related to my persistence layer architecture or if they’re still inherent to the ModelActor itself. I’ve generally followed the blog posts: https://fatbobman.com/en/posts/practical-swiftdata-building-swiftui-applications-with-modern-approaches/ and https://brightdigit.com/tutorials/swiftdata-modelactor/ and aim to achieve the following: I have a single DataProvider that holds the ModelContainer and uses it to configure and initialize a single DataHandler. These are created once at app launch and injected into the SwiftUI view hierarchy as EnvironmentObjects. Since I need to access the SwiftData models not only in SwiftUI but also indirectly in ViewModels or UIKit views, all read operations on the models should go through the DataProvider ( ModelContainrs MainCont
4
0
1.5k
Dec ’24
Reply to 0xBAADCA11 Occurs when VoIP Call Incoming (APNs Push Notification)
If Wi-Fi is enabled when the sXGP-SIM is implemented, the crash that in step 8 of the [Operation] in the first explanation will occur 100%. Have you looked at exactly what happens when your app launches in that configuration? I don't see how this would change the systems routing for pushes, but I could see it distorting your apps own network logic. Also, how is the app being distributed to the device? I do not that there are extra verification steps for enterprise apps and, in theory, it's possible that issues with that verification process could delay the app launch long enough that callservicesd then terminates the app. my crash log was below. code:0xBAADCA11 explanation: Yes, that's the termination code. My question was what did the rest of the log show? Is there a way to send the logs other than uploading them directly here? You can either file a bug and post the number back here, or you can file a code level support request and send them through email. However, this is what I would generally prefer: If n
Topic: App & System Services SubTopic: General Tags:
Apr ’25
Reply to Need 3.1.3(f) Guidelines Clarification
Based on your quote and description, I would agree with your interpretation and confusion. But it is important to remember power discrepancies in any relationship. For all but a few developers, Apple holds all the power and can interpret e.g. to mean whatever it wants. I'm curious now. App Review says that they have resolved your issue. What was the resolution? What does e.g. mean in the context of the Apple App Review Guidelines? Does it mean for example or specifically?
Apr ’25
Reply to Error when running a modelContext rollback
Hi - I am getting the same error, but on modelContext.delete(). I went through checking all the relationship ids, and they are all fine. All models seem to be registered and can be accessed through relationships, fetches or @Query. . I have had some success with SwiftData, using in a semi-production app for about a year. I have never seen this error message before. This seems like a new bug to me.
Replies
Boosts
Views
Activity
May ’25
Document-based SwiftData apps fail to identify a store on iPad?
When trying to run my document-based iPad app using iPadOS 18 beta and Xcode 16 beta, I get an error like the following after opening a document: Thread 1: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData from [:] In order to help track down what is going wrong, I downloaded the sample app from WWDC23 session Build an app with SwiftData found here: https://developer.apple.com/documentation/swiftui/building-a-document-based-app-using-swiftdata When I try to run the end-state of that sample code, I get a similar error when running the app on my iPad and creating a new deck: Thread 1: Fatal error: Failed to identify a store that can hold instances of SwiftData._KKMDBackingData from [:] Given that the sample project is generating the same error as my own project, is this a problem with SwiftData and document-based apps in general? Or is there a change of approach that I should try?
Replies
19
Boosts
0
Views
2.9k
Activity
Feb ’25
SwiftData updates in the background are not merged in the main UI context
Hello, SwiftData is not working correctly with Swift Concurrency. And it’s sad after all this time. I personally found a regression. The attached code works perfectly fine on iOS 17.5 but doesn’t work correctly on iOS 18 or iOS 18.1. A model can be updated from the background (Task, Task.detached or ModelActor) and refreshes the UI, but as soon as the same item is updated from the View (fetched via a Query), the next background updates are not reflected anymore in the UI, the UI is not refreshed, the updates are not merged into the main. How to reproduce: Launch the app Tap the plus button in the navigation bar to create a new item Tap on the “Update from Task”, “Update from Detached Task”, “Update from ModelActor” many times Notice the time is updated Tap on the “Update from View” (once or many times) Notice the time is updated Tap again on “Update from Task”, “Update from Detached Task”, “Update from ModelActor” many times Notice that the time is not update anymore Am I doing something wrong? Or is
Replies
1
Boosts
0
Views
834
Activity
Apr ’25
How to import large data from Server and save it to Swift Data
Here’s the situation: • You’re downloading a huge list of data from iCloud. • You’re saving it one by one (sequentially) into SwiftData. • You don’t want the SwiftUI view to refresh until all the data is imported. • After all the import is finished, SwiftUI should show the new data. The Problem If you insert into the same ModelContext that SwiftUI’s @Environment(.modelContext) is watching, each insert may cause SwiftUI to start reloading immediately. That will make the UI feel slow, and glitchy, because SwiftUI will keep trying to re-render while you’re still importing. How to achieve this in Swift Data ?
Replies
2
Boosts
0
Views
147
Activity
Apr ’25
Reply to How to import large data from Server and save it to Swift Data
One way would be to perform the import in the background in a @ModelActor. Something like this: @ModelActor actor ImportService { func import(data: [Data]) throws { for date in data { let model = Model(/* ... */) modelContext.insert(model) } try modelContext.save() } } This way the import will not block the UI, and the imported data will only be visible in the UI after modelContext.save() is called. I haven't used SwiftData in a real-world app yet, however, so do be careful. For example I don't know if SwiftData will keep the models in memory until save() is called, so you might have to save more frequently. But even then, the UI will not update for every newly inserted model but only when you choose.
Replies
Boosts
Views
Activity
Apr ’25
Reply to iOS 16.3 in Simulator
Definitely an issue in xCode 16.3 as it applies to inherited properties and the simulators. I can't diagnose why it's happening, but there are cases where the value shown in the debugger is not what it should be and that causes code to fail. What's crazy is that looking in the debugger at self.inheritedProperty and then expanding self and drilling down to inheritedProperty, the values do not match. The value on the self node is correct. The issue does not occur on a real device and Xcode 16.0 is still solid as it applies to simulators. Personally, I will be avoiding xCode 16.3 until Apple issues an update that addresses a fix for this.
Replies
Boosts
Views
Activity
Apr ’25
Reply to How to import large data from Server and save it to Swift Data
Do you know how much data is expected to be downloaded? If so, you could ensure the view doesn't update until the right amount of data is received and stored. If you don't know the quantity of data, you could add a Bool and toggle it when the data loading starts, then toggle it when the data has finished loading, so the View is only refreshed when the last of the data is stored. Another way would be to disable the UI so the user can't interact with it until the data has finished loading. The UI would update in the background, but the user wouldn't experience a slow UI as they can't interact with it anyway. You could put a partially-transparent View with a ProgressView in it at the top of a ZStack (which would be at the bottom of the ZStack in the code...) and hide it when you're done. I'll show you how I do it below. There's a few ways of doing it, but I don't think there's a built-in way to achieve this. But then, I'm not a genius on SwiftData, so... Using a blocking view: @State private var loading
Replies
Boosts
Views
Activity
Apr ’25
Question about BGAppRefreshTask approach for medication scheduling app
I'm developing a medication scheduling app similar to Apple Health's Medications feature, and I'd like some input on my current approach to background tasks. In my app, when a user creates a medication, I generate ScheduledDose objects (with corresponding local notifications) for the next 2 weeks and save them to SwiftData. To ensure this 2-week window stays current, I've implemented a BGAppRefreshTask that runs daily to generate new doses as needed. My concern is whether BGAppRefreshTask is the appropriate mechanism for this purpose. Since I'm not making any network requests but rather generating and storing local data, I'm questioning if this is the right approach. I'm also wondering how Apple Health's Medications feature handles this kind of scheduling. Their app seems to maintain future doses regardless of app usage patterns. Has anyone implemented something similar or can suggest the best background execution API for this type of scenario? Thanks for any guidance you can provide.
Replies
2
Boosts
0
Views
193
Activity
Apr ’25
SwiftData and SpotLight Search
Hi all, has anybody found the trick how to get SwiftData working with SpotLight Search? Setting the attribute spotlight in the Model definition seems to do nothing at all, as pointed out by Paul Hudson in his new book as well (https://www.hackingwithswift.com/quick-start/swiftdata/how-to-index-swiftdata-objects-in-spotlight) Thanks a lot!
Replies
3
Boosts
0
Views
1.1k
Activity
Apr ’25
SwiftData rollback not updating the UI
I'm building a simple App using SwiftData. In my app a user can create, remove and edit posts. When editing, I want them to be able to hit Save to persist the changes or Cancel to discard the changes. The approach I'm using is to disable autosave and call modelContext.save() when saving and modelContext.rollback() when discarding the changes. So my modelContainer is defined as follows: WindowGroup { ContentView() .modelContainer(for: [Post.self], isAutosaveEnabled: false) } and I Save and Cancel like this: PostForm(post: post) .toolbar { ToolbarItemGroup(placement: .cancellationAction) { Button(Cancel) { if modelContext.hasChanges { modelContext.rollback() } dismiss() } } ToolbarItemGroup(placement: .confirmationAction) { Button(Save) { do { if modelContext.hasChanges { try modelContext.save() } } catch { fatalError(Failed to save post: (error.localizedDescription)) } callback?() dismiss() } } } The issue I am facing is that after calling modelContext.rollback() my Posts aren't updating in the UI, th
Replies
2
Boosts
0
Views
565
Activity
Jul ’24
Reply to SwiftData error: NSKeyedUnarchiveFromData' should not be used to for un-archiving and will be removed in a future release
I am receiving the same errors on Xcode 16.3 (16E140), using SwiftData but only simply types like String and Date with no transformations.
Replies
Boosts
Views
Activity
Apr ’25
Ongoing Issues with ModelActor in SwiftData?
After the problems with the ModelActor in iOS 18, it seemed like the ModelActor became more stable with iOS 18.1 and macOS 15.1. However, I’m still encountering many problems and crashes. I wanted to ask if these issues are related to my persistence layer architecture or if they’re still inherent to the ModelActor itself. I’ve generally followed the blog posts: https://fatbobman.com/en/posts/practical-swiftdata-building-swiftui-applications-with-modern-approaches/ and https://brightdigit.com/tutorials/swiftdata-modelactor/ and aim to achieve the following: I have a single DataProvider that holds the ModelContainer and uses it to configure and initialize a single DataHandler. These are created once at app launch and injected into the SwiftUI view hierarchy as EnvironmentObjects. Since I need to access the SwiftData models not only in SwiftUI but also indirectly in ViewModels or UIKit views, all read operations on the models should go through the DataProvider ( ModelContainrs MainCont
Replies
4
Boosts
0
Views
1.5k
Activity
Dec ’24
Reply to 0xBAADCA11 Occurs when VoIP Call Incoming (APNs Push Notification)
If Wi-Fi is enabled when the sXGP-SIM is implemented, the crash that in step 8 of the [Operation] in the first explanation will occur 100%. Have you looked at exactly what happens when your app launches in that configuration? I don't see how this would change the systems routing for pushes, but I could see it distorting your apps own network logic. Also, how is the app being distributed to the device? I do not that there are extra verification steps for enterprise apps and, in theory, it's possible that issues with that verification process could delay the app launch long enough that callservicesd then terminates the app. my crash log was below. code:0xBAADCA11 explanation: Yes, that's the termination code. My question was what did the rest of the log show? Is there a way to send the logs other than uploading them directly here? You can either file a bug and post the number back here, or you can file a code level support request and send them through email. However, this is what I would generally prefer: If n
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Apr ’25
Reply to Need 3.1.3(f) Guidelines Clarification
Based on your quote and description, I would agree with your interpretation and confusion. But it is important to remember power discrepancies in any relationship. For all but a few developers, Apple holds all the power and can interpret e.g. to mean whatever it wants. I'm curious now. App Review says that they have resolved your issue. What was the resolution? What does e.g. mean in the context of the Apple App Review Guidelines? Does it mean for example or specifically?
Replies
Boosts
Views
Activity
Apr ’25
SwiftData lazy loaded list
Are SwiftData queries lazy loaded when used in conjunction with SwiftUI List? @Query var posts: [PostModel] List { ForEach(posts, id: .id) { post in PostView(post) } } If the code above is not lazy loaded, how can we make it lazy loaded?
Replies
3
Boosts
0
Views
636
Activity
Nov ’24