|
I have run into an issue regarding Core Data. I am trying to follow the MVVM pattern here. I have broken down into a very simple example code and I'd like to find what's the problem here. ViewModel
Content View
Core Data Model: Only has one Entity called "TestEntity" with two attributes, name and date. It seems like calling "fetchEntries" is not allowed. What's my mistake here? |
|
|
|
|
|
Hi Julian I confirm, that it is not working if you use the
As the wrapper takes the managed object context from the environment, the property wrapper is only available in the SwiftUI views. (At least to my knowledge, there is no other place where those environment objects exist/work).
You will probably have to get back to
|
|
Have a look at this: https://augmentedcode.io/2020/01/19/using-coredata-with-swiftui/amp/ I Have tested the code and can confirm it works. The solution is to make teh ViewModel an inner class of the main View, that way it has access to the property wrapper e.g:
|
|
I was having the same issue and this really helped! Thank you! One question, I don't understand why you need to create a your own model class isn't what Core Data is for? |
|
I only use the "ViewModel" class as a convenient place to aggregate the different |
|
Hello paul, i like your teaching very much and am one of your students. regarding this topic:
|
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.