1

I'm trying to figure out the best way to implement MVC design in my application. Right now when a user enters the UITableViewController, I send a request to my Model to go to the internet and download some data. When the data returns, I want it to update the data even if my UITableViewController is dealloced (by the user closing the view, or backing out of it). So currently I update the model and then have a delegate on the model to "update the UI" on the UITableViewController. Basically after the data returns, I want the model to call [tableView reloadData] and I achieve this with a delegate.

My questions:

  1. Is this necessarily the correct way? Is there any other way for my model to call methods on my UIViewController?

  2. What if I want multiple ViewControllers to be notified when that model is changed...for example, what if I back out of UITableViewController1 and then navigate into UITableViewController2 and that same model/data is used. Should I just be creating multiple delegates on my model?

Just looking for some best practices and wondering if I'm doing this correctly.

Thanks in advance.

VTS12
  • 452
  • 8
  • 22

2 Answers2

1

You could either use NSNotification to alert listeners of the changes, or Key-Value Observing (KVO). Both are flavors of the Observer Pattern which will help you remove coupling from your model to your views and as a bonus allow any number of observers to find out about changes to your model without having to tell them all individually.

There's automatic and manual change notification. This explains the difference.

I would wager you would do manual notification, so you would do:

[self willChangeValueForKey:@"something"];
... // update 'something' here
[self didChangeValueForKey:@"something"];

And the observers would get the notification. The automatic method requires use of attribute methods that you probably wouldn't use internally. Usually KVO applies to a single attribute of the model, of course there could be many within the model sending notifications. If you're doing something more along the lines of "I just completed process X", it's probably more of a NSNotification.

Nick Veys
  • 23,458
  • 4
  • 47
  • 64
  • For KVO, after I parse the XML data from the web request and change my model, it will automatically notify the VCs that register for change notifications? At that time if the model is an array of objects, I could call tableView:insertRows:atIndexPaths? – VTS12 May 23 '12 at 16:59
  • Yikes... Formatting doesn't work so well in comments. I updated the response to add some info about manual/automatic KVO. – Nick Veys May 24 '12 at 17:37
0

answering question 2 - if you have some objects, that should be notified about some event, you should use NSNotifications (Observer pattern). That helps you to make these objects react, update their views or so, in easy way

medvedNick
  • 4,512
  • 4
  • 32
  • 50
  • I've read that using NSNotifications can make an application very difficult to manage. We have A LOT of HTTP requests going in and out for multiple models. Is this still a good idea or should this be done with multiple delegates? Is that even possible? – VTS12 May 23 '12 at 16:49
  • you can see [here](http://stackoverflow.com/questions/1927965/nsnotificationcenter-vs-delegation-using-protocols) for difference between them. As I know, all code written with notifications you can rewrite with delegates. In your case, I think notifications will be easer way to do than delegates – medvedNick May 23 '12 at 21:48