Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Use UIImage in ViewModel(from MVVM) and in Presenter(from MVP)

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:

  • It's a simple image loader
  • Flow: Loads JSON with pictures from 500px -> Shows cells in UICollectionView depends on count pictures in returned JSON -> Gradually downloads pictures and displays it in cells
  • The same applications I'm implementing under 3 architecture designs MVVM, 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

  1. Can I use import UIKit in ViewModel/Presenter?
  2. What is the best place for Load&Store UIImage in this UIViewController, ViewModel/Presenter or something else?

GitHub link on sample application

MVVM Diagram MVP Diagram

like image 769
Slavik Voloshyn Avatar asked Jul 21 '17 14:07

Slavik Voloshyn


People also ask

What is the difference between MVP and MVVM in iOS?

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.

Why MVVM is better than MVP iOS?

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.

How MVVM is better than MVP?

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.

Why MVVM is better than MVC iOS?

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.


1 Answers

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.

like image 86
AJ B Avatar answered Nov 15 '22 00:11

AJ B