views:

39

answers:

3

Hi all,

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

A: 

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.

Kenny
sorry, I think you misunderstand about the views, they are in difference view controllers.
Thanh-Cong Vo
A: 

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.

Mo
Uhmmm, I think that guy ask for who will call the NSNotificationCenter to do the job you described above. And he wants to ask about software design
vodkhang
hmm, thanks. The question was edited after I started posting, so I didn't see the changes
Mo
+2  A: 

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.

Anurag
yep, in my case, the model knows when the task completes, and it does callback the view controller who requests to perform that task. So I don't want it send Notification to other view controllers. Because that mean the model has to care about the view. I think the view controller who receives callback should send that notification.Is that good? Correct me if my thought is bad.
Thanh-Cong Vo
okie, I've just read your update. Thank you very much :)
Thanh-Cong Vo