|
Hey, I am new to SwiftUI (2.0) and I'm trying to follow the MVVM pattern. While trying to implement CoreData, I ran into some issues. While I can access my data from the View, I can't find out how I would access it from my ViewModel. Can give me a tip how to make the implementation of CoreData conform to MVVM? MyApp.swift:
MainView.swift:
|
|
hi, Core Data stores Entities that have Attributes (which include relationships among the Entities). the PersistentContainer that you access on disk/cloud that holds the Core Data store happens to be named "Model." as far as i understand the MVVM design in the presence of Core Data, all of Core Data is basically your Model. when you set up a @FetchRequest in a View, that provides access to some/all entities in Core Data of a certain type, and so is a window into your Model, and thus it can often function as the ViewModel inside the View, since its role in life is to keep the View updated when the Core Data objects in the model behind the fetch request change. in your code, you appear to have an Entity named "MyModel" and so the @FetchRequest variable so i'd suggest that a little renaming might be of help here (too many things are called something like "model"). and what attributes constitute a "MyModel" entity? when we see the MyModel definition (and any other Core Data entities you have or intend to have), we can probably say more. hope that helps, DMG |
|
Hi @delawaremathguy Thanks for your quick answer! Currently I'm using UserDefaults to save my model, but I want to transition this to using CoreData. My CoreData entity is base on the following model:
This model is then linked to the view with the following viewmodel:
The viewmodel has a variable where all instances of my model are stored and a computed property that has to access these instances of my model, since I don't want to make these calculation in the view. I'm not sure how I would implement this computed variable else. Therefore I wanted to first load the instances from coredata into a viewmodel. L4M |
|
hi, so, what you do is create in Core Data an Entity named "Account," with attributes exactly as you have in the struct representation. Do this in the data model editor -- except for one caveat: Core Data does not have any natural understanding of what is a Color. assume for the moment that we bypass this detail: but you may wish to see more about storing colors in Core Data in this thread on the Apple Developer Forums. when you build the project, XCode will generate the necessary support files for you, which provides a class at this point, you'll need to do a few things.
where to put this? put it in your MainView and call it as a modifier on your view using now your MainView can build out a List or something using the accounts variable, using a ForEach(accounts) mechanism. How do you find the files generated by XCode for the Account class? right-click on Account in the code above and do Jump to Definition. this will show you the (minimal) class file. and then right-click on one of the fields of Account, such as that's enough here, i think, to digest. but if you want to see something related I have out there for public consumption, take a look at my sample ShoppingList app. i'm not a Core Data expert by any means, and i'm still working my way into SwiftUI (this project has made me ask so many questions that i have learned a lot, even if it does not yet show in the code). it is not a SwiftUI app like the new WWDC2020 -- it still has an AppDelegate and SceneDelegate, but i think your App structure above covers the basic injection of the managed object context into the MainView already. oh, and did i forget to mention: i store some colors in Core Data (well, sort of: i store the RGBA components as Doubles). hope that helps, DMG |
|
Thanks again! This post helped me understand the topic alot better. I will try to implement it as soon as I can. For saving colors I found a solution using Have a nice day :) L4M |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.