From the course: SwiftData Essential Training

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Modify model context

Modify model context

- [Instructor] The one caveat about this model container, you lose autoSave. We've already set our views to use programmatic savings, so that doesn't affect us. However, you might want to add autoSave to your project. So, let's close all this up here and head over to model container. In our model container code, we have no mention of model context. It is not an environment variable. We don't access it that way. Instead we make a model context object from the model container. We're going to take our model container that we have here. I'm going to copy it and I'm going to paste it underneath. And we'll call it autoSaveModelContainer. After our model container declaration here, I'm going to put another one, let modelContext = ModelContext for that model Container. Model context has only one modifiable property, autosaveEnabled. By default, it is false, so I'm going to change it to true. And that would mean we'd have autoSave again. There's one more use for this model context you might…

Contents