I am trying to start using MVVM
with Objective-c
but I get some problems with CoreData
. I don't know who should handle the fetchedResultsControllerDelegate
methods. The viewModel
or the viewController
?
I think that the viewModel should handle it, but I see too much code to do the same.
ViewModel is bad and wrong with SwiftUI If you find the MVVM pattern in SwiftUI troublesome or not so useful in your implementation, it may be because the MVVM design pattern does not fit the characteristics of SwiftUI. Until now, iOS application development has often employed the MVVM architecture.
Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.
A controller that you use to manage the results of a Core Data fetch request and to display data to the user.
Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.
Let's say you have an edit button in a view. Edit button is hidden when there are no objects in fetchResultsController and you want to show it when any objects are added.
If you implement NSFetchedResultsControllerDelegate
in viewController:
If you implement NSFetchedResultsControllerDelegate
in viewModel:
If you implement these methods in viewController then it gets reference to the model and this should not happened in MVVM
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
I think that correct solution is to implement the NSFetchedResultsControllerDelegate
in ViewModel. Then you should have similar protocol to notify the viewController to update the view, but it should work with viewModels, not with the models
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With