I'm trying to figure out in MVVM and MVP architecture designs. I've decided to write simple one screen application with different design approaches.
Few words about sample application:
UICollectionView
depends on count pictures in returned JSON -> Gradually downloads pictures and displays it in cellsMVVM
, MVC
and MVP
Everything was going smoothly until I've needed to download images and display them 😅 .
Initially I've though that the best place for handling this logic and caching pictures is ViewModel
(from MVVM
) and in Presenter
(from MVP
) but AFAIK these two entities should be independent from UI layer, but if we want to use UIImage
there it means that we should import UIKit
in ViewModel
/Presenter
🤔
So, questions
import UIKit
in ViewModel
/Presenter
?UIImage
in this UIViewController
,
ViewModel
/Presenter
or something else?GitHub link on sample application
View model in MVP has a user interface, activity and fragments of data, and it interacts with the presenter. View model in MVVM has no business logic at all and has only a user interface. There is a ViewModel in MVVM where the business logic is located.
If your application has some screens that's changes a lot in its state based on the model at the runtime, then it is better to use MVVM, because MVVM always holds the View's state in addition to the binding will make the change in UI easier, because when you update your ViewModel the UI will be updated automatically.
MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing. All my projects(written in Kotlin for Android app) are based on MVVM.
KEY DIFFERENCE In MVC, controller is the entry point to the Application, while in MVVM, the view is the entry point to the Application. MVC Model component can be tested separately from the user, while MVVM is easy for separate unit testing, and code is event-driven.
Instead of working with UIImage objects why not use the image Data? That way you do your caching and logic working with the Data and then convert to UIImage objects when you need to. So the only time you work with UIImage will be in the UIViewController converting the Data to UIImage.
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