Hello everyone. I have a question related to MVVM and CoreData. I previously called/updated coredata entities inside Views, but soon realised that the code was getting messy. So I decided to use MVVM to implement a clean architecture. After watching a couple of tutorials, I made a sample model for one of my entities. I am not sure whether I am doing this in the right way or not. The code sample works perfectly. I used this as a Model and I used a ModelView of the View to call this. May you be able to check this code and tell me what can be improved, please? The code is self-explanatory, but if you need more information, please ask. (I am new to Swift)
Also, if the entity has a large amount of data, calling the read function within the Init() is not efficient. In that case, I assume the best thing to do is the use of NSPredicate along with the relevant parameters to the project.
apart from some naming issues (e.g., a
my suggestions would be a slight rewrite of write, update, and delete, plus removing the need to keep re-reading the data from Core Data, would be
without knowing what else you're doing in the app, this will probably work for you as long as all CRUD operations go through the view model.
however, if you ever use NSPersistentCloudKitContainer to share data across personal devices (which implies some cloud syncing happening in the background), or if your app has other views where you make changes to dogs in the database while the View associated with your view model is still alive in SwiftUI, this view model will not see external changes.
hope that helps,
Thanks @delawaremathguy. Your answer helped a log.
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.
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.