Search code examples
iosmvvmdelegateskey-value-observingdelegation

iOS MVVM: Is Delegation Pattern a Requirement?


First of, researching MVVM for iOS is a headache as there are multiple varying implementations floating around online. So far I found 3 categories of MVVM:

  1. MVVM that uses Delegation / Protocols to update the ViewController / View
  2. MVVM with KVO binding like ReactiveCocoa
  3. MVVM as a simple helper / wrapper class for the model (no protocols, no KVO)

Is Delegation Pattern or KVO a requirement for implementing MVVM in iOS?

I've seen a number of MVVM examples online that do not make use of Protocols / Delegation, and instead just use MVVM as a helper class to encapsulate the Model and provide state to the ViewController.

Is that still considered MVVM?


Solution

  • I totally agree with you, it's really a painful job to get correct understanding about MVVM on internet as everyone has their own implementation, I had the same situation as yours.

    It does not really matter which approach are you following to implement MVVM as long as you are not violating below rules:

    View:

    1. Owns ModelView
    2. Manages view cycle.
    3. Handel user interaction and pass actions to ModelView.

    ModelView:

    1. It owns Model
    2. Perform all business operations.
    3. It notifies to View on updates

    Model:

    1. It notifies to ModelView on updates.

    So as per the rules above either you can go with 1st or 2nd point as 3rd is not applicable here because without binding or notifying you cannot achieve MVVM.

    Hope it helps.