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

Комментарии • 26

  • @nwalter08
    @nwalter08 Год назад +5

    I have been stuck on this for weeks, thank you so much!!

  • @frankvandevoort
    @frankvandevoort 8 месяцев назад

    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!

  • @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 11 месяцев назад +2

    Works Fine - one change simple:
    in previewContainer ...
    let container = try ModelContainer(for: Todo.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true))
    Thanks 😍

  • @ErwinFoekema
    @ErwinFoekema 11 месяцев назад

    This solved my struggles with SwiftData & Previews, tnx

  • @sanjeev3799
    @sanjeev3799 6 месяцев назад

    Thank you Karin!

  • @luisgomezreus732
    @luisgomezreus732 Год назад +1

    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)
    }
    }

  • @sergiovinhal5934
    @sergiovinhal5934 5 месяцев назад

    Thank you so
    Much !

  • @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?

  • @ErwinFoekema
    @ErwinFoekema 9 месяцев назад

    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?

  • @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"?

  • @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!

  • @artemis99999
    @artemis99999 Год назад

    Great thanks!

  • @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.

  • @vitaliizadubnyi1021
    @vitaliizadubnyi1021 10 месяцев назад

    Thank a lot

  • @magpro8775
    @magpro8775 Год назад

    Amazing 🥰

  • @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