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

  Рет қаралды 10,957

Karin Prater

Karin Prater

Күн бұрын

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 жыл бұрын
How do I like this video a million times?
@keatsp
@keatsp Жыл бұрын
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 Жыл бұрын
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! 😄👍
@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!!
@garychen8185
@garychen8185 3 жыл бұрын
I have been binging your videos on SwiftUI and I want to leave a comment to commend you for these excellent content.
@LeviM337
@LeviM337 3 жыл бұрын
Wow god bless you! Thank you so much.
@SwiftyPlace
@SwiftyPlace 3 жыл бұрын
You're very welcome!
@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!
@seccat
@seccat 2 жыл бұрын
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 жыл бұрын
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!
@SwiftyPlace
@SwiftyPlace 3 жыл бұрын
My pleasure!
@luisgomezreus732
@luisgomezreus732 2 жыл бұрын
Very nice video. Thank you very much Karin.
@SwiftyPlace
@SwiftyPlace 2 жыл бұрын
Glad you liked it!
@antonsergeyev2624
@antonsergeyev2624 3 жыл бұрын
Thanks a lot!
@matthiasrudnick9888
@matthiasrudnick9888 3 жыл бұрын
Thanks a lot.
@phongphong1986
@phongphong1986 2 жыл бұрын
thank you for sharing
@fulviofagnani8395
@fulviofagnani8395 3 жыл бұрын
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 3 жыл бұрын
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 жыл бұрын
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.
@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?
@MsEva112
@MsEva112 2 жыл бұрын
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 жыл бұрын
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.
@deepakgautam1893
@deepakgautam1893 2 жыл бұрын
Please use dark mode in Xcode in future videos.
@yourgflikesit
@yourgflikesit Жыл бұрын
How to do core data migrations though?
Is CoreData better without MVVM?
11:03
Flo writes Code
Рет қаралды 1,3 М.
Concurrency of SwiftData, by Donny Wals (English)
31:08
CocoaHeadsNL
Рет қаралды 2,1 М.
Clown takes blame for missing candy 🍬🤣 #shorts
00:49
Yoeslan
Рет қаралды 49 МЛН
Useful gadget for styling hair 🤩💖 #gadgets #hairstyle
00:20
FLIP FLOP Hacks
Рет қаралды 10 МЛН
How to safely work with Core Data on a background thread
5:36
Swift Arcade
Рет қаралды 8 М.
Core Data One-To-Many SwiftUI
20:54
Swift Arcade
Рет қаралды 7 М.
SwiftUI 2.0: MVVM - A Practical Approach
14:28
BeyondOnesAndZeros
Рет қаралды 32 М.
I've been using Redis wrong this whole time...
20:53
Dreams of Code
Рет қаралды 347 М.
This is how senior iOS devs use CoreData from different modules | ED Clips
9:09
Core Data In SwiftUI | SwiftUI Core Data Tutorial
2:29:44
tundsdev
Рет қаралды 22 М.