5. Swift Data One to Many Relationships

Поделиться
HTML-код
  • Опубликовано: 7 июл 2024
  • This is the 5th video in the SwiftData series and the second in this section.
    In this video I want to introduce a one to many relationship for our books to allow us to add multiple pull quotes for each book.
    If you want to support my work, you can buy me a coffee :)
    ko-fi.com/stewartlynch
    Table of Contents:
    0:00 Introduction
    1:18 The Quote Model
    2:52 One to Many Relationships
    5:53 Quotes List View
    21:56 Cascade deletion
    Starter Project Files:
    github.com/StewartLynch/MyBoo...
    Completed Project:
    github.com/StewartLynch/MyBoo...
    All videos in this section
    SwiftData: Lightweight Migrations
    • 4. SwiftData Lightweig...
    SwiftData: One To Many Relationships
    • 5. Swift Data One to M...
    Recommended Resources:
    BigMountain Studio Books
    SwiftUI Views Mastery
    www.bigmountainstudio.com/vie...
    SwiftUI 5 Book Bundle
    www.bigmountainstudio.com/boo...
    Combine Mastery in SwiftUI
    www.bigmountainstudio.com/com...
    Core Data Mastery in SwiftUI
    www.bigmountainstudio.com/cor...
    SwiftUI Animations Mastery
    www.bigmountainstudio.com/ani...
    AppCoda Books
    Beginning iOS 16 Programming with Swift (SwiftUI + UIKit)
    gumroad.com/a/646790611/WihuX
    Beginning iOS 16 Programming with Swift (SwiftUI + UIKit) - Starter
    gumroad.com/a/646790611/mOzKq
    Mastering SwiftUI 4 + Swift Programming for iOS 16 - Professional
    gumroad.com/a/646790611/gjPBw
    Mastering SwiftUI 4 - Starter
    gumroad.com/a/646790611/TyNEz
    Other References:
    Stewart Lynch's RUclips Channel:
    / @stewartlynch
    Stewart Lynch on Mastodon:
    iosdev.space/@StewartLynch
    Stewart Lynch On Threads:
    @StewartLynch
    Stewart Lynch on X:
    x.com/StewartLynch
    Stewart Lynch on LinkedIn
    / stewartlynch
  • НаукаНаука

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

  • @yurii_chynchyk
    @yurii_chynchyk 7 месяцев назад +1

    Another great Tutorial in the Series. Thanks a lot, Stewart!
    But I noticed a BUG with deletion of the quote. Say I create 3 quotes inside the book. "Sample quote 1", 2 and 3. Then I delete the third of those. So 1 and 2 should remain. But after going back in Navigation to BookList and then again into EditBookView and to it's quotesListView I see that the wrong quote has been deleted instead the one I deleted. Quote 2 disappeared while 1 and 3 remained. It stays the same after relaunching the App. And in SQLBrowser I see the same outcome.
    Did I mess something in code during tutorial or you have that Bug too? Can you please check?
    Can it be related with Sorting of quotes perhaps ?
    thanks a lot for answer !

    • @StewartLynch
      @StewartLynch  7 месяцев назад +2

      Good Catch.... I don't know what I was thinking. I will update the source code here and pin this comment.
      The correct code should be this:
      .onDelete { indexSet in
      withAnimation {
      indexSet.forEach { index in
      let quote = sortedQuotes[index]
      book.quotes?.forEach{ bookQuote in
      if quote.id == bookQuote.id {
      modelContext.delete(quote)
      }
      }
      }
      }
      }

    • @yurii_chynchyk
      @yurii_chynchyk 7 месяцев назад

      @@StewartLynch It’s fine, no worries! Sometimes we all miss.
      Thank you so much, Stewart!

    • @andrejkling3886
      @andrejkling3886 7 месяцев назад +1

      All works like Swiss watches...⛑@@StewartLynch

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

    Excellent series…. Chugging along, haven’t lost me yet!

  • @andrejkling3886
    @andrejkling3886 7 месяцев назад +1

    Thank you so very much Stewart …💯♨️

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

    This is fantastic and has saved me from so much pain!

  • @TimMitra
    @TimMitra 7 месяцев назад

    Another great tutorlial, with tons of value

  • @gccount
    @gccount 4 месяца назад

    sorted quotes array is very practical, this is the key . i haven't found in the other videos.

  • @mangoalphacon616
    @mangoalphacon616 7 месяцев назад

    Thank you for the video. I learned a lot. Although I wish the emphasize would have been more on the concept of relationships. Relationships are a very complex topic in data modeling. A general introduction would be very helpful. An explanation and demonstration of all the deletion rules and their pitfalls would be great too. That’s incredibly important to get right, because it can mess up a database immensly. Your Swift Data series has been a great help for me so far. A game changer was to check my data in the DB Browser app. I caught so many bugs that way and started to really grasp how Swift Data works. As soon as the data model gets more complicated like in a real world app Swift Data is not easy at all, but can get very very messy. Especially without an object graph showing all the relationships in the data model. Working blindly with Swift Data is equivalent to jumping into a deep abyss of darkness. Thank you for the torch!

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

    Thank you

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

    Thanks Stewart, that’s clearly explained. So, was the quote array in Book made optional because of the default “nullable” rule for implicit relationships? If so, once it was changed to “cascade” delete in the explicit relationship macro, could we make the Book’s quote property a non-optional array?

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

      No. It has to be optional for CloudKit and I will get to that later.

  • @chezchezchezchez
    @chezchezchezchez 7 месяцев назад

    I’m so upset. I’m working on a major project with swift data, and I just had to completely give up all the relationships and just Store “ids” in the parent object.
    (I was having all kinds of problems with speed and disappearance of data, probably because I didn’t have things set up properly)
    So now, what do you think? Am I going to have huge problems ignoring relationships?
    So I now have a UUID for an ID of an object, and I store that in an array of the other object. It seems to work, but I’m wondering what big problems I’m gonna run into.

    • @jorgeantoniomuchicazapana7493
      @jorgeantoniomuchicazapana7493 7 месяцев назад

      Si los persistentIdetifier no se pueden grabar directamente a pesar de que dicen que es codable, yo he creado una estructura independiente y ahi lo guardo ejemplo Chioice { id: PersistentIdentifier, state: EnumState }

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

    Thank you Stewart. Great illustration of the one to many connexion, quite different from the classical database descriptions..
    One question: In the old days we spoke about master-slave relations between tables. I understand that this is now an expression which is no longer used. Do you know which expressions ar now mainstream to describe the relation between tables ?

  • @rickywitherspoon2861
    @rickywitherspoon2861 7 месяцев назад

    Great video. Can you add a video to this series about seeding the database so there are books when the user launches the application for the first time?

    • @StewartLynch
      @StewartLynch  7 месяцев назад

      I do that in the second video for the Preview so the process would be the same for the real stores database.

  • @andrejkling3886
    @andrejkling3886 17 дней назад +1

    In modelContainer(container) like modelContainer(for: Book.self) should we add Quote like modelContainer(for: [Book.self, Quote.self]) ?

    • @StewartLynch
      @StewartLynch  17 дней назад +1

      Not necessary since Book has a relationship to Quote

    • @andrejkling3886
      @andrejkling3886 17 дней назад +1

      @@StewartLynch thank you

  • @timothycbryant
    @timothycbryant 3 месяца назад

    We've gone over dynamically sorting the list of books, but say I wanted to dynamically sot the quotes as well. Maybe by page number, or date in reverse. How would the logic work to give the user several sort options for the quotes?

    • @StewartLynch
      @StewartLynch  3 месяца назад

      This is currently problematic using a @Query in SwiftData. You might want to do this as an in memory sort using a sort method.

    • @timothycbryant
      @timothycbryant 3 месяца назад

      @@StewartLynch ah that's a bummer but thanks for the response. Maybe there'll be an update to that at WWDC this year... along with CloudKit Sharing hopefully.

  • @kukizk
    @kukizk 3 месяца назад

    18:56 how did you changed `NavigationLink (...)` to `NavigationLink {...} label: {...}`?

    • @StewartLynch
      @StewartLynch  3 месяца назад +1

      I think I just hit enter on the keyboard and let Xcode simplify the trailing closures. I have a complete video on understanding trailing closures that you might want to take a look at.

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

    How can I find the folder path to the sqlite database if it does not show in the console window? Is that a setting I have to turn on to see that folder path at minute 05:20

    • @StewartLynch
      @StewartLynch  5 месяцев назад +1

      I show this in the first video of this series
      ruclips.net/video/CAr_1kcf2_c/видео.htmlsi=uc5MqDTZ2tprwIKx&t=330

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

      @@StewartLynch Thank you! I guess I should have been patient and started at the beginning of the series!

  • @user-gt2uz9jn7e
    @user-gt2uz9jn7e 5 месяцев назад

    What is the reason to create an Array as Optional when you can use empty Array in this case?

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

      I think I mentioned that I will be implementing CloudKit and it you are, all relationships must be optional. I agree, an empty array would have been nice, but unfortunately if you want to use CloudKit, it is not an option.

    • @user-gt2uz9jn7e
      @user-gt2uz9jn7e 3 месяца назад

      @@StewartLynch Oh! Interesting feature... 😂 Thanks you for the answer!

  • @peterbien2879
    @peterbien2879 3 месяца назад

    associative attribute of a many to many relation
    Object patterns can have any number of attribute patterns and vice versa.
    Example:
    Object pattern: Ventilation system has several
    Attribute pattern: Power
    Power with the title "electrical connected load" and the unit "kW"
    Output with the title "Ventilation output" with the unit "m3/h"
    How would you implement this with SwiftData?

    • @StewartLynch
      @StewartLynch  3 месяца назад

      I cover many to many relationships in a later video

    • @peterbien2879
      @peterbien2879 3 месяца назад

      Sorry for my mistake, I asked the question about the wrong video. I had already asked a similar question there 3 months ago. Since no one has responded to it yet, I have rephrased my question.

  • @obelix8477
    @obelix8477 7 месяцев назад +1

    At minute 20:52: +10 points for the @twoStraws reference and hint about adding inflection rule to a string element. Nice!
    At minute 21:19: -4 points for misinformation. How does the inflection rule "properly capitalize" the word?

    • @StewartLynch
      @StewartLynch  7 месяцев назад

      -10 for using the wrong word LOL. Glad you are not my teacher. Of course I meant pluralize. Oops. And in my defence, I do have an overlay that clearly uses the word plural. Oops.