Creating, editing, and deleting model objects - SwiftData Starter Project 4/8

Поделиться
HTML-код
  • Опубликовано: 13 окт 2024
  • Add swipe to delete and @Bindable to enable adding, editing, and deleting SwiftData model objects. You can find the completed source code here: github.com/two...
    This is an 8-part project that walks you through building a SwiftData project from scratch using SwiftUI. It's designed to work alongside my book SwiftData by Example, available at www.hackingwit...
    This project is aimed at programmers who have some experience with Swift and SwiftUI already, and want to get up to speed with SwiftData as quickly as possible.

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

  • @alessandromanilii6458
    @alessandromanilii6458 Год назад +3

    On everyday job I'll use it in 10 years, but... in my own personal apps, I'll use it as soon as possible. Amazing video as usually! ... even because you are paying homage to my country :)
    Thank you very much!

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

      I hope they upgrade before that! How old are the oldest phones that people use as their primary phone? Hopefully not 10 years old. That would be the iphone 5s. My wife stopped using that in 2019.

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

      @@markaurelius61 I totally agree with you... I work as consultant in one of the biggest banking apps here in Italy. 85% of the users have iOS 16+, and less than 0.1% uses iOS12. During the beta phase of iOS17 we had more users with 17 than 12. We asked many many times to drop 12 but they always refused without giving a real reason.

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

      @@asanokatana Yes, I expect CoreData to keep going for a good number of years

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

    I did your 100 days with Swift, it is so good to learn with you like in the old days and with the most recent content 🚀

  • @stephenwalsh3643
    @stephenwalsh3643 8 месяцев назад +2

    I don't understand the implementation of addDestination with regards to the path. It works as expected, but why does it work? 🤪

  • @DavidKababyan
    @DavidKababyan 11 месяцев назад +3

    The Add new desstination is not correct in my opinion. What if the user clicks on add new, and then clicks back button? we will have empty Destination item in SwiftData and in our list.

    • @AD-wg8ik
      @AD-wg8ik 11 месяцев назад

      This is just a learning demo. That can easily be fixed by moving the modelContext.insert(destination) functionality into the EditDestinationView.

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

    Thank you so much Paul, you're a legend! This makes managing data so much easier! 😀

  • @japonska2009
    @japonska2009 8 месяцев назад +1

    I dont understand this path thing. what and how does it do this "magic"?
    and with navigation, why dont we use:
    NavigationLink(destination: EditDestinationView(destination: destination)) {
    VStack(alignment: .leading) {
    Text(destination.name)
    .font(.headline)
    Text(destination.date.formatted(date: .long, time: .shortened))
    }
    If i try to add a new destination, i get an yellow triangle displayed (error maybe?) on the editDestination view

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

    Why do we use a For-in loop in the deleteDestinations function, when we can delete only one item sometimes?

  • @DD-ds7ui
    @DD-ds7ui 6 месяцев назад +1

    In don't understand addDestination() magic? what is happening here? how Destination() getting values? let destination = Destination(). we are not passing any values here...what is happening?

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

    Question: if you have an array of 10 items, and you want delete items 1, 4, 7...... when you delete item 1 from the list, won't that change the indices for the remaining items and so 4 and 7 will no longer be the correct indices? If you delete them in reverse order, ok, but.....

  • @qweasd321a
    @qweasd321a Год назад +2

    How do we prevent blanks from being inserted? For example, if I add a new Destination then hit back right away without inputting any text. I don’t want a blank showing up there.

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

      same question

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

      Same question, please respond

    • @MirrorsEdgeGamer01
      @MirrorsEdgeGamer01 8 месяцев назад +1

      Sorry for this being late. This is how I fixed it.
      .onAppear
      {
      for destination in destinations {
      if destination.name == ""
      {
      modelContext.delete(destination)
      }
      }
      }
      I attach this to the ListView. Sorry, for the formatting; I am on my iPad.

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

      @@MirrorsEdgeGamer01 no prblm thanks for you respons

    • @petecampbell963
      @petecampbell963 2 месяца назад

      @@MirrorsEdgeGamer01 Where exactly do you put this code? where is the ListView?

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

    Hello Paul, great tutorial! For some reason my Xcode preview is crashing. Did you have that issue?

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

      Mine is crashing as well. Did you find a solution?

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

      @@morgancooledge3557 hi, you should add #Preview in this video to the ContentView preview. You may remove example line and instead of return Edit…. you should add ContentView.

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

      Try:
      #Preview {
      ContentView()
      .modelContainer(for:Destination.self)
      }

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

    I have an issue, I'm not sure if it's a bug with Swift Data, where in the simulator my list of names is always the name of the first model added to the context. Each time I delete one, it goes to the second oldest one (like going through a queue). This bug(?) doesn't happen in the preview.
    Eg. If my modelContext has dest1, dest2, dest3 added in that order, it will look like
    dest1
    dest1
    dest1
    Then if I delete any of them it'll look like
    dest2
    dest2
    Then if I delete either of those it'll be
    dest3

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

    I get an error on the .modelContainer(container) line:" Cannot convert value of type 'some View' to closure result type 'UIView'" with the code:
    do {
    let config = ModelConfiguration(isStoredInMemoryOnly: true)
    let container = try ModelContainer(for: Destination.self, configurations: config)
    let example = Destination(name: "Example Destination", details: "Example details go here and will automatically expand vertically as they are edited.")
    return EditDestinationView(destination: example)
    .modelContainer(container)
    } catch {
    fatalError(error)
    }

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

    Question about your delete action and it's placement. You use .onDelete on the ForEach loop and hence need the Index. I used a .swipeAction with a button to delete the destination on the VStack. Both delete the correct Destination. Is there any advantage/disadvantage of one over the other?

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

      I prefer onDelete() because it enables list editing using EditButton, but as we aren't using it here there's no real difference.

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

    I have a basic answer (sorry if its too basic). I've trying to use Swift Charts to build data visualisations. I have always used structs to model data for the purpose of charts. Here we are defining data models using Classes in SwiftData. Will there be any difference in how I use swift Charts with this? Thanks in advance!

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

      Classes can be operated remotely (such as:
      var example = example(value: 0)
      example.value += 1 (the value of Example here is increased by 1)
      example().value += 1 (the value of Example here is 1 more, and it operates on the same object
      print(example.value)
      ⬆will print out 2
      )
      Each time struct is used, a new object is added.
      For example:
      var structExample = StructExample(value: 0)
      var structExample2 = StructExample(value: 0)
      ⬆Add new object of structExample(). The changes here are changes to the variable and will not affect other objects.
      structExample.value += 1 (changing this variable will not affect other objects using StructExample)
      strcutExample2.value += 1
      print(structExample.value)
      ⬆will print out 1
      print(structExample2.value)
      ⬆will print out 1
      Different methods (classes or struct) will be used to create models for different usage environments, such as:
      When creating chartData, because adjustments need to be made to individual data, it is better to use struct to build the model.
      The class can be used to build a viewModel because of its remote controllability.
      這是我用google翻譯翻譯來的,如果用詞上有不精準的部分請見諒:D

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

    Question 2: even though you can pass an IndexSet, I've never seen an example where more than one item is deleted at the same time. i've never seen a screen where you select multiple times and then click delete once and have it delete all items. usually it's just swipe left to delete, but one at a time.
    can you make a video to show a multiple delete situation? or point me to one?

    • @MirrorsEdgeGamer01
      @MirrorsEdgeGamer01 2 месяца назад

      @@DavidNitzscheBell It could be useful for a photo grid view.

  • @CompoundStudios-h5p
    @CompoundStudios-h5p 8 месяцев назад

    @Bindable doesn't work for me when placing the Model inside of an array

  • @40YOStudent
    @40YOStudent Год назад

    Hard to distinguish between the destination of a navigation link and the destination of a trip.

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

    I really don't understand why it is
    return EditDestinationView(destination: .example)
    .modelContainer(container)
    instead of
    return EditDestinationView(destination: .example)
    .modelContainer(container)

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

    Awesome🎉