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.
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.)
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!
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?
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?
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
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.
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?
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.
How do I like this video a million times?
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! 😄👍
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.
Thanks you solved my NSInternalInconsistencyException problem, in the first 3 minutes. I was save my persistence context too often.
Excellent! Good to know that this is crucial information. I was already wondering why this topic is not talked more about.
Wow god bless you! Thank you so much.
You're very welcome!
I have been binging your videos on SwiftUI and I want to leave a comment to commend you for these excellent content.
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!!
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!
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.)
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!
Not here. It's not a Stanford. But, you can download codes from Stanford lectures where she take ideas.
Great video. I learned a lot about code structure with Core Data and SwiftUI.
Very nice video. Thank you very much Karin.
Glad you liked it!
Thx for your work!
My pleasure!
You’re the best! Thanks a lot 🙏🏼
amazing job, thx
Thanks a lot!
Thanks a lot.
thank you for sharing
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?
Good question! The keyboard shortcut is control + i
You can also have a look at more shortcuts in this list peterfriese.dev/xcode-shortcuts/
Please use dark mode in Xcode in future videos.
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?
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
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.
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?
Is there a way to have the @fetchrequest tucked away in the view model?
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.
How to do core data migrations though?