I find the question a bit confusing. Firstly, are you talking about saving and fetching objects in general or is it the specific image property of type Data you mean when you write “data” in the question? Secondly it’s very hard to understand the last segment of code.
Do you get any error message in the console when you save?
Could you replace try? with try when saving to see if any error is thrown
do {
try modelContext.save()
} catch {
print(error)
}
How is your model container declared and used?
When you are using @ModelActor you have access to a specific model context for that actor via the property modelContext so you should have
try modelContext.delete(…
and you should definitely not use the context from the main actor in any other actor.
This alone will probably not fix the crash since you have a reference to the deleted objects in self._rooms that you need to clear before doing the delete.
One option is to create and work with a local instance of ModelContext in your view and then call save() or rollback (or simply discard it) on that local instance. Once you call save the changes will be propagated to the main context.
One article about this can be found on https://www.hackingwithswift.com/quick-start/swiftdata/how-to-discard-changes-to-a-swiftdata-object or as part of this longer article, https://medium.com/@matgnt/the-art-of-swiftdata-in-2025-from-scattered-pieces-to-a-masterpiece-1fd0cefd8d87
I would wrap the UserSettings type inside an observable class and then use that class as an environment value
@Environment(SettingsManager.self) var settingsManager
Then this manager object could be made to handle all loading, updating and saving of the UserSettings object internally to have only one single source of truth.
Unrelated perhaps but why are you even using SwiftData for this, wouldn't it be better to use a CSV (or JSON) file for persisting your data?
Maybe I am misreading the code but to me it looks like you only want to store a single instance of your model and then I believe SwiftData or any other database solution is overkill and that a simple file is more suitable.
First of all you should check the console and check if you get any warning messages when you run the app and access your data.
if nothing shows up then you can turn on sql debug logging to help you figure out what is happening. See this article from the Hacking With Swift website on how to do that, https://www.hackingwithswift.com/quick-start/swiftdata/using-launch-arguments-to-debug-swiftdata-and-core-data
I find the question a bit confusing. Firstly, are you talking about saving and fetching objects in general or is it the specific image property of type Data you mean when you write “data” in the question? Secondly it’s very hard to understand the last segment of code.
Do you get any error message in the console when you save?
Could you replace try? with try when saving to see if any error is thrown
do {
try modelContext.save()
} catch {
print(error)
}
How is your model container declared and used?
When you are using @ModelActor you have access to a specific model context for that actor via the property modelContext so you should have
try modelContext.delete(…
and you should definitely not use the context from the main actor in any other actor.
This alone will probably not fix the crash since you have a reference to the deleted objects in self._rooms that you need to clear before doing the delete.
One option is to create and work with a local instance of ModelContext in your view and then call save() or rollback (or simply discard it) on that local instance. Once you call save the changes will be propagated to the main context.
One article about this can be found on https://www.hackingwithswift.com/quick-start/swiftdata/how-to-discard-changes-to-a-swiftdata-object or as part of this longer article, https://medium.com/@matgnt/the-art-of-swiftdata-in-2025-from-scattered-pieces-to-a-masterpiece-1fd0cefd8d87
I would wrap the UserSettings type inside an observable class and then use that class as an environment value
@Environment(SettingsManager.self) var settingsManager
Then this manager object could be made to handle all loading, updating and saving of the UserSettings object internally to have only one single source of truth.
Unrelated perhaps but why are you even using SwiftData for this, wouldn't it be better to use a CSV (or JSON) file for persisting your data?
Maybe I am misreading the code but to me it looks like you only want to store a single instance of your model and then I believe SwiftData or any other database solution is overkill and that a simple file is more suitable.
First of all you should check the console and check if you get any warning messages when you run the app and access your data.
if nothing shows up then you can turn on sql debug logging to help you figure out what is happening. See this article from the Hacking With Swift website on how to do that, https://www.hackingwithswift.com/quick-start/swiftdata/using-launch-arguments-to-debug-swiftdata-and-core-data