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

preview_player
Показать описание
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

If you liked what you learned and you want to see more, check out one of my courses!
👩🏻‍💻 Combine course

#SwiftUI #CoreData
Рекомендации по теме
Комментарии
Автор

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.

keatsp
Автор

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

Rggg-lemon
Автор

How do I like this video a million times?

tayloratheo
Автор

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! 😄👍

coolcodingdad
Автор

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

garychen
Автор

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

seccat
Автор

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!

tayloratheo
Автор

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

mhfs
Автор

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!

fulviofagnani
Автор

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

HilaProdomo
Автор

Very nice video. Thank you very much Karin.

luisgomezreus
Автор

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?

jasperdunn
Автор

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?

MsEva
Автор

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.

notorioustofu
Автор

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 {
_ =
}
}

or

private func addItem() {
withAnimation {
_ = Item.addItem(context: viewContext)
}
}

Is there something that could go wrong if I do it in this way?

achimreiske
Автор

Please use dark mode in Xcode in future videos.

helloworld_sw
Автор

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

Rggg-lemon
visit shbcf.ru