MVC pattern implementation

I am having a problem implementing an MVC template in iOS using Swift. According to Apple documentation, there is an MVC scheme:

MVC pattern

I am fine with this, but, as you can see, when the model changes itself (an incoming message from a socket, for example), how should it notify the controller?

For example, I have a chat application with a model that represents a list of messages. When the model receives a new message, how does it notify the controller? Is there a common way to do this?

thanks

+5
source share
2 answers

You can achieve this Model-Controller communication in two ways.

For a detailed explanation, I would recommend that you take a look at the CS 193p MVC lecture. ( https://www.youtube.com/watch?v=Cb8KtEI3ZaY )

+3
source

Communication between layers is a very interesting topic and guarantees not only a list of methods.

Here is a very important article from objc.io , which not only contains an exhaustive list of communication methods, but also analyzes their strengths and weaknesses and offers a flowchart to help you decide which method is best.

Right choice

In your case, the model is the sender, and the controller is the receiver. Typically, the controller holds the model, so the controller knows the Model, but the Model does not know the Controller. Thus, you will be taken to the bottom of the chart.

Please read the full article. It also has examples taken from Apple. This is really helpful.

+2
source

All Articles