How to use SwiftData with Previews - Xcode 15 beta 5 - Preview Container and Sample Data
HTML-код
- Опубликовано: 20 окт 2024
- In this SwiftUI tutorial, I show you some tricks on how to set up SwiftData with the preview feature in Xcode. Working with SwiftData in Xcode previews can be challenging, especially when there is no data available to visualize what you’re building. In this video, I demonstrate how to overcome this problem by using SwiftData to set up a preview container with pre-populated data.
By setting up a model container in memory, I ensure that the preview generates new data each time it refreshes. To add data to the container, I create a new instance of a model container and use a main context to generate the necessary instances.
Please note that the information I provide in this video is based on Xcode 15 Beta 5 and may become outdated in the future. However, the concepts and techniques I discuss can still be applied to newer versions of Xcode.
I am using macOS Ventura 13.4 and Xcode 15 beta 5
⬇️ Github project github.com/gah...
⭐️ This code was mainly used from the official demo app for SwiftData :developer.appl...
#swiftdata #xcode #preview
I have been stuck on this for weeks, thank you so much!!
Happy to help!
Many thanx Karin, this exactly solved my issue with the "failed to find a currently active container" error I was getting on Previews that required some model data as input!
Thank you very much for the video. I'm starting a new project with swiftdata and I wasn't sure how the previews worked until I saw your video. Thank you very much again.
Works Fine - one change simple:
in previewContainer ...
let container = try ModelContainer(for: Todo.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true))
Thanks 😍
Thanks man!
This solved my struggles with SwiftData & Previews, tnx
Thank you Karin!
An idea to select the persistModel is to pass a key and a value to filter the query.
struct ModelPreview2: View {
var key: KeyPath
var value: String
var content: (Model) -> Content
init(key: KeyPath,
value: String,
@ViewBuilder content: @escaping (Model) -> Content) {
self.key = key
self.value = value
self.content = content
}
var body: some View {
PreviewContentView(key: key, value: value, content: content)
.modelContainer(previewContainer)
}
private struct PreviewContentView: View {
@Query private var models: [Model]
var key: KeyPath
var value: String
var content: (Model) -> Content
var body: some View {
if let model = filtered.first {
content(model)
} else {
ContentUnavailableView("Couldn't load model for preview", systemImage: "xmark")
}
}
private var filtered: [Model] {
models.filter { $0.getValue(forKey: key) == value }
}
}
}
and use like this
#Preview() {
ModelPreview2(key: \Person.legalId, value: "12345678N") { model in
PersonView(person: model)
.modelContainer(previewContainer)
}
}
Thank you so
Much !
Hi, I’m continuing to study SwiftData by repeating all the tutorials but I can’t understand one thing:
I have to create an app with a default list (which therefore no data needs to be added) and this list is contained in a toggle. How do I make only the toggle state persistent?
The previews work fine. But when I want to upload to xcode cloud I get
Cannot find 'ModelPreview' in scope or Cannot find 'previewContainer' in scope
How to solve this?
Nice how fast you're aboard the SwiftData train. As a new swift dev, swiftdata seems like a very good way forward.
Do you know why Swiftdata models can not conform to "Codable"?
Awesome - this works, but when I modify a view the preview now crashes, and I need to delete all stale app data using Terminal to get it working again. Do you have any ideas as to what I need to fix?
Worked it out! After making the container in previewContainer, I needed to add a ModelConfiguration with isStoredInMemoryOnly: true, and now it works perfectly!
Great thanks!
Anyone know how to activate the undo manager for a model context if you are passing a previewContainer (or any other custom container) to the .modelContainer modifier? The syntax for isUndoEnabled: true only works if you pass 'for: [ModelA.self, ModelB.self, etc.], isUndoEnabled: true' into the .modelContainer modifier. But doing that defeats the purpose of the custom container.
Thank a lot
Amazing 🥰
Thanks 🤗
How would you do the same thing with a view that needs a bindable object like an edit view?
I figured this out by changing from @Binding to @Bindable in the view thanks to another video of yours! Thanks!
hello Karin please answer my email I just send it from your website