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 controll开发者_如何学编程ers.
Thanks
From your description it looks like the model knows when the task completes, so the model should be responsible for generating that notification.
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
I don't understand what you mean here. The whole purpose of using notifications via NSNotificationCenter
is 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:
1. View Controller 1 subscribes to "onTaskFinished" message at NotificationCenter
2. View Controller 2 subscribes to "onTaskFinished" message at NotificationCenter
3. User presses "Perform Task" button in some view controller
4. Model Manager gets triggered and performs the task
5. Model Manager publishes "onTaskFinished" message to NotificationCenter
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.
Have a look into Core Data for starters.
Otherwise: After the the the model manager performs its task, have your view controller call [view setNeedsDisplay]. This will force all of your views/tables etc. to redraw themselves, and in the process, they should retrieve their information from the data model and display its current contents.
You can send notification to an instance of NSNotificationCenter
, like this:
// after completing the task
[[NSNotificationCenter defaultCenter] postNotificationName:@"noteName" object:self userInfo:optionalDic];
Before sending this, your views or any other files can register to receive the notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someName:) name:@"noteName" object:notification sender];
Your object's someName:
method is then called with an instance of NSNotification
as the argument when you post the notification.
精彩评论