How to use SwiftData with Previews - Xcode 15 beta 5 - Preview Container and Sample Data

  Рет қаралды 4,582

Karin Prater

Karin Prater

Күн бұрын

Пікірлер: 26
@frankvandevoort
@frankvandevoort 9 ай бұрын
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!
@nwalter08
@nwalter08 Жыл бұрын
I have been stuck on this for weeks, thank you so much!!
@SwiftyPlace
@SwiftyPlace Жыл бұрын
Happy to help!
@luisgomezreus732
@luisgomezreus732 Жыл бұрын
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.
@RafaelGuima
@RafaelGuima Жыл бұрын
Works Fine - one change simple: in previewContainer ... let container = try ModelContainer(for: Todo.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true)) Thanks 😍
@allenwixted1992
@allenwixted1992 11 ай бұрын
Thanks man!
@ErwinFoekema
@ErwinFoekema 11 ай бұрын
This solved my struggles with SwiftData & Previews, tnx
@sanjeev3799
@sanjeev3799 7 ай бұрын
Thank you Karin!
@sergiovinhal5934
@sergiovinhal5934 5 ай бұрын
Thank you so Much !
@luisgomezreus732
@luisgomezreus732 Жыл бұрын
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) } }
@artemis99999
@artemis99999 Жыл бұрын
Great thanks!
@vitaliizadubnyi1021
@vitaliizadubnyi1021 11 ай бұрын
Thank a lot
@magpro8775
@magpro8775 Жыл бұрын
Amazing 🥰
@SwiftyPlace
@SwiftyPlace Жыл бұрын
Thanks 🤗
@piteke100
@piteke100 9 ай бұрын
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?
@gmjelle
@gmjelle Жыл бұрын
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"?
@ErwinFoekema
@ErwinFoekema 10 ай бұрын
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?
@noahzsol
@noahzsol Жыл бұрын
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?
@noahzsol
@noahzsol Жыл бұрын
Worked it out! After making the container in previewContainer, I needed to add a ModelConfiguration with isStoredInMemoryOnly: true, and now it works perfectly!
@kevinkivo
@kevinkivo Жыл бұрын
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.
@gordongudmundson4982
@gordongudmundson4982 Жыл бұрын
How would you do the same thing with a view that needs a bindable object like an edit view?
@gordongudmundson4982
@gordongudmundson4982 Жыл бұрын
I figured this out by changing from @Binding to @Bindable in the view thanks to another video of yours! Thanks!
@zardashtjaza1343
@zardashtjaza1343 Жыл бұрын
hello Karin please answer my email I just send it from your website
How Xcode 15's 6 Preview Features Will Revolutionize Your Development
8:27
Rebeloper - Rebel Developer
Рет қаралды 2,8 М.
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
Não sabe esconder Comida
00:20
DUDU e CAROL
Рет қаралды 62 МЛН
Introduction to SwiftCharts: Creating Charts and Graphs in SwiftUI
48:20
How To Use SwiftData with Preview | SwiftData Tutorial | #10
26:16
CloudKit - Intro, Pricing, Terms, & Definitions
17:26
Sean Allen
Рет қаралды 23 М.
SwiftData Migrations | SwiftData Tutorial | #11
32:24
tundsdev
Рет қаралды 4,6 М.
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН