How to best use Core Data with SwiftUI 2.0 - MVVM - Unit test and working with the preview

Поделиться
HTML-код
  • Опубликовано: 1 авг 2024
  • In this tutorial, I am discussing ways on how to use Core Data with SwiftUI 2.0. I will use the template generated by Xcode for Core Data. In order to get a correct updating of the UI it is very important to consider Core Data entities as classes. In SwiftUI we can use the @FetchRequest property wrapper.
    I will try to use MVVM design pattern in a different way. The goal is to write clean code and include unit test.
    00:00 intro
    02:16 What is MVVM?
    05:05 Xcode template with Core data
    20:27 using extensions for convenience initialisers
    26:27 Core data entities can act like view models
    34:02 optional attributes and syntactic sugar
    39:11 core data working with the preview
    47:14 unit tests
    54:30 save content with ScenePhase going into background
    01:00:06 wrap up
    Watch more
    MVVM SwiftUI tutorial: • How to use MVVM in iOS...
    Stanford course SwiftUI and Core Data Part 1: • Lecture 11: Picker
    Stanford course SwiftUI and Core Data Part 2: • Lecture 12: Core Data
    If you liked what you learned and you want to see more, check out one of my courses!
    👨‍💻 my SwiftUI course school.swiftyplace.com/course...
    👨‍💻 my Core Data and SwiftUI course school.swiftyplace.com/course...
    👩🏻‍💻 Combine course
    school.swiftyplace.com/course...
    #SwiftUI #CoreData

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

  • @tayloratheo
    @tayloratheo 2 года назад +2

    How do I like this video a million times?

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

    AMAZING. I am a long time app developer with many apps that extensively implement CoreData. This is BY FAR the best tutorial I have ever seen and I have seen ALOT. I am immediately implementing most of this into my apps. Thank you so much.

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

    Excellent video Karin! You helped me understand how to work with Core Data in SwiftUI and how to test it to the next level! Thank you so much! 😄👍

  • @garychen8185
    @garychen8185 3 года назад +1

    I have been binging your videos on SwiftUI and I want to leave a comment to commend you for these excellent content.

  • @tayloratheo
    @tayloratheo 2 года назад

    This has honestly help my cut down on 33% of the code I've been writing and it's only been a week since I watched the video. Thanks much!

  • @LeviM337
    @LeviM337 3 года назад +5

    Wow god bless you! Thank you so much.

  • @danielwatson6529
    @danielwatson6529 2 года назад

    I think this is the one for me! watching in silence because i for got my head phones on the train. will nail this when i get home!!

  • @seccat
    @seccat 2 года назад +1

    Thanks you solved my NSInternalInconsistencyException problem, in the first 3 minutes. I was save my persistence context too often.

    • @SwiftyPlace
      @SwiftyPlace  2 года назад

      Excellent! Good to know that this is crucial information. I was already wondering why this topic is not talked more about.

  • @HilaProdomo
    @HilaProdomo 3 года назад

    Great video. I learned a lot about code structure with Core Data and SwiftUI.

  • @mhfs61
    @mhfs61 2 года назад +2

    Hi Karin,
    Thank you for this video. It’s a confirmation of something I came across and I couldn’t fix, the way it was apparently supposed to be done.
    After trying to implement MVVM in the standard way (using CoreData and CloudKit) without having several issues, I finally came to the same solution you’re proposing. I still wasn’t sure whether it was the a good solution, though it worked. So again thank you for this confirmation.
    Furthermore I also use a temporary struct for each entity. That temporary struct is populated, when displaying the entity detail view. In the detail view I collect the changes made in the temporary struct and only save the changes when the save/update button is tapped. The save/update button is only activated when any change is made. When the save/update button is active and tapped, the entity is populated with the collected data from the temporary struct. This prevent the listview to display changes that where not persisted in the database (one of the several issue I came across.)

  • @consumer8592
    @consumer8592 3 года назад

    You’re the best! Thanks a lot 🙏🏼

  • @arthurgeniere432
    @arthurgeniere432 2 года назад

    amazing job, thx

  • @user-gr3kt8uk3c
    @user-gr3kt8uk3c 3 года назад

    Thx for your work!

  • @antonsergeyev2624
    @antonsergeyev2624 3 года назад

    Thanks a lot!

  • @luisgomezreus732
    @luisgomezreus732 2 года назад

    Very nice video. Thank you very much Karin.

  • @matthiasrudnick9888
    @matthiasrudnick9888 3 года назад

    Thanks a lot.

  • @phongphong1986
    @phongphong1986 2 года назад

    thank you for sharing

  • @fulviofagnani8395
    @fulviofagnani8395 3 года назад +9

    Hi Karin! Very interesting adaptation of MVVM, especially when dealing with swiftui and core data. Is there somewhere to download this code example, this would be very useful. Thanks so much for these great tutorials!

    • @dmitriykhan2011
      @dmitriykhan2011 2 года назад

      Not here. It's not a Stanford. But, you can download codes from Stanford lectures where she take ideas.

  • @jasperdunn
    @jasperdunn 2 года назад

    Hey Karin, thanks again for the excellent explanations throughout your tutorials! Just wondering, how do you auto-format the code? is there a shortcut you're using?

    • @SwiftyPlace
      @SwiftyPlace  2 года назад +1

      Good question! The keyboard shortcut is control + i
      You can also have a look at more shortcuts in this list peterfriese.dev/xcode-shortcuts/

  • @notorioustofu3482
    @notorioustofu3482 2 года назад

    I found that if test_fetch method's `var item = Item(context: context)` is commented out, the test will throw false positives when setting the testing scheme to execute in random order and running all tests a few times. I assumed that since empty is held in memory, the item created in test_Add_Item is still in memory. Adding `addTeardownBlock { context.delete(item) }` at the end of test_Add_Item resolves the issue.

  • @MsEva112
    @MsEva112 2 года назад +1

    how you solved the problem in Unit Test part is not clear, your code is changed suddenly. I am getting the a run time error too. Do you know what the problem is and how to solve ? Did you change the static empty var in Persistence file?

    • @notorioustofu3482
      @notorioustofu3482 2 года назад

      First, she deleted both testing targets and create a new one (File>New>Target>Unit Testing Bundle). If you do the same and put the code in as it was originally, it throws errors on the PersistenceController variable. If you look at the code after her cut away, she's PersistenceController instance into the function itself. I'd try the code change before deleting the targets

  • @danielwatson6529
    @danielwatson6529 2 года назад

    Is there a way to have the @fetchrequest tucked away in the view model?

    • @SwiftyPlace
      @SwiftyPlace  2 года назад +1

      That would be great. But when I tried it, my app crashed. I think the problem is that it need the NSManagedContext in the SwiftUI environment. The view model does not 'see' the view environment.

  • @achimreiske1964
    @achimreiske1964 2 года назад

    Hi Karin, at first I have to thank you for your very good videos.
    To this video I have some questions. Maybe you can explain it to me.
    You have the functions to add and to delete Item(s). You have the delete func in the item extension and the add func in the view.
    Wouldn't it be better to have both in the same place? And you always have to call the PersistenceController.save method.
    Is it possible to have both functions inside the PersistenceController or in the ItemExtensiion and also call the save in there?
    Then you would have the addItem func in the view only like this:
    private func addItem() {
    withAnimation {
    _ = PersistenceController.shared.addItem()
    }
    }
    or
    private func addItem() {
    withAnimation {
    _ = Item.addItem(context: viewContext)
    }
    }
    Is there something that could go wrong if I do it in this way?

  • @deepakgautam1893
    @deepakgautam1893 2 года назад +2

    Please use dark mode in Xcode in future videos.

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

    How to do core data migrations though?