6

I am new to iOS development, so I would appreciate some feedback.

I am trying to build an iOS client for my web service. So far this is what I have done:

I am implementing two views (Utility-based app using Storyboard). In the main view, I use a text field and a search button, where the user can enter a query and then click the search button. Once the search button is clicked, my intention is to read the value of the text field, and use it in my Restful call to my web service. My web service replies back with a JSON file with the query results, which I parse and show to the secondary view's text area.

I know how to do the restful call in iOS and how to do the JSON parsing as well as displaying the results on the screen (at least the text stuff, but that's another different question). But my intention is to learn and implement MVC basics to my application.

According to MVC, the controller updates the view, and the model sends out a notification broadcast which the controller can listen to and know if there are any changes in the object. So this is what I would ideally like to do:

My Model - My model would handle the core RESTful call, get the JSON reply, parse it and get the resulting values that I want to display on the view.

My Controller - I would like my controller to listen to my model and obtain the resulting values from Model and display them on View.

Using a quick and dirty way, I can implement the RESTful call, JSON parsing and displaying resulting values - all inside the Controller, but with this technique, if my view changes tomorrow, then I have to re-write my code. Or if I want to add new features, then I have to change my controller. So ideally I would like to have a core Model that's not aware of how View looks like, and just let's the Controller take the results from Model and display them on View.

From what I have read from Google search results so far, two ways of doing this is by a) Key Value Observation and b) Notification center.

For last 2 days, I am trying to find a good decent way to implement Notification center or read more about it, I am not getting a good lead. Some of the questions I have is, can I send out the String results value using Notification center that my controller picks up? How does Notification Center really work with string values? Where can I find some good examples?

So any help regarding this will be very much appreciated.

BlueChips23
  • 1,861
  • 5
  • 34
  • 53

2 Answers2

12

Some of the questions I have is, can I send out the String results value using Notification center that my controller picks up?

Yes, that would commonly done using the userInfo property of a NSNotification. userInfo is a plain NSDictionary that may contain instances of NSObject derived objects indexed by keys that are adhering to the NSCopying protocol (commonly NSString is used). Note that the dictionary (userInfo) will retain your parameter object/s.


How does Notification Center really work with string values?

Well, that depends on how you want it to work. But nitpicking aside, see below.


Where can I find some good examples?

Maybe this one helps...


Example

The receiver (controller) registers for the notification:

- (void)registerForNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(modelObjectUpdatedString:)
                                                 name:@"StringUpdated"
                                               object:nil];
}

The sender (model) notifies the world:

- (void)stringUpdateWith:(NSString *)theString
{
    self.string = theString;
    [[[NSNotificationCenter defaultCenter] postNotificationName:@"StringUpdated" 
                                                         object:self 
                                                       userInfo:[NSDictionary dictionaryWithObjectsAndKeys:self.string, @"String", nil]];
}

The receiver (controller) receives the notification within its handler:

- (void)modelObjectUpdatedString:(NSNotification *)notification
{
    ModelObject *postingObject = [notification object];
    NSString *string = [[notification userInfo]
        objectForKey:@"String"];
    ...
}
Till
  • 27,559
  • 13
  • 88
  • 122
  • 2
    This is a perfect answer. Thank you so much for a clear and concise answer. This is exactly what I was looking for. – BlueChips23 Apr 16 '12 at 00:12
4

You're thinking along the right path, but still not entirely. As Till "points out" in his comment, you should not handle the RESTful communication inside your model. If I were you, I would create a utility class responsible for fetching the information, and then a class responsible for holding the data(this last class is your model).

It would be clever to create a class method that allocates and initiates a new instance of this object, created from the JSON data fetched through your RESTful communicator class.

From your controller point of view:

RESTHelper *rest = [RESTHelper restHelperWithURL:yourRESTURL];
YourModel *model = [YourModel modelWithJSON:[rest fetchObjectWithID:1]];
// Present your models data in the view.

You may benefit from using CoreData here, and I strongly encourage you to look into that.

rhummelmose
  • 647
  • 4
  • 15
  • Good tips here. Thanks for guiding me in the right direction. These are the kinds of feedback I was hoping to get, since I am new to iOS development, and I want to get the basics right, before doing any complicated apps. – BlueChips23 Apr 16 '12 at 00:14
  • You shouldn't use NSNotificationCenter unless you have a case where blocks, KVO or delegation cannot do the job. – rhummelmose Apr 16 '12 at 23:00
  • Hi Rasmus, I'm also getting to grips with IOS. (Not so new to OO). Would you then suggest that the restHelper class "Calls back" to a protocol on the Model, once it's got some data? And could you explain why you use [rest fetchObjectWithID:1]? I know I came across it in the Stanford videos, but it seems I've destroyed those brain cells... – Greycon Jan 21 '13 at 20:53