I want to ask about software design.
I have a task, the view controller handles UI event for calling a model manger to perform that task. After finishing, the model manager will callback to update the view.
There have also other view controllers who care about that task, and also want to update its own view when that task is finished. So I register a Notification for that task in each view controllers.
The problem is defining where should I send “finishing task” Notification, in Model manager or in the view controller who handles event and receives the callback from Model manager?
What is better design? Should the model care about send this “common” task, or should the view controller?
I think it’s better to choose View controller, but my friend says that a view controller shouldn’t care about other view controllers.
Thanks
From your description it looks like the model knows when the task completes, so the model should be responsible for generating that notification.
I don’t understand what you mean here. The whole purpose of using notifications via
NSNotificationCenteris to decouple the sender from the receiver. The model just publishes a message to the notification center, and interested subscribers will receive the message via the notification center. The view controllers don’t need to directly interact with the model manager for this.Here’s one possible workflow:
On Application Startup:
View Controller is a bad choice for sending this message as it couples the view controllers together, and proper functioning of your app depends on the proper functioning of this view controller.
Consider the case where this view controller gets unloaded because it was taking up too much memory. Now the other view controllers will not receive the “task finished” notification because the view controller that was responsible for sending this notification in not in memory anymore, and not listening for notifications obviously.