Reputation: 301
I have a problem to implementing the MVC pattern in iOS with Swift. According to the Apple documentation, there is a MVC schema:
I am fine with that but as you can see, when the model changes itself (an incoming message from a socket, for exemple) how is it supposed to notify the controller?
For instance, 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 conventional way to do that?
Thanks
Upvotes: 5
Views: 635
Reputation: 10608
Communication between layers is a very interesting topic, and warrants more than just a list of methods.
Here is a very relevant article from objc.io that not only has an exhaustive list of communication methods, but also analyses their strength and weaknesses and suggests a flowchart to help you decide which method is best.
In your case, the Model is the sender, and the Controller is the recipient. Usually, the controller holds the model, so the Controller knows the Model but the Model is ignorant of the Controller. You would therefore be in the lower part of the chart.
Please read the full article. It also has examples taken from Apple frameworks. It's really useful.
Upvotes: 2
Reputation: 4085
You can achieve this Model - Controller communication in two ways.
For detailed explanation I would recommend you to watch CS 193p MVC lecture. (https://www.youtube.com/watch?v=Cb8KtEI3ZaY)
Upvotes: 3