|
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.
|
|
hi, 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, DMG |
|
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.