Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communication between Model and Controller - iOS

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.

like image 860
BlueChips23 Avatar asked Apr 15 '12 23:04

BlueChips23


People also ask

What is model view controller in IOS?

MVC is a software development pattern made up of three main objects: The Model is where your data resides. Things like persistence, model objects, parsers, managers, and networking code live there. The View layer is the face of your app. Its classes are often reusable as they don't contain any domain-specific logic.

What is the difference between model and controller?

The model is responsible for managing the data of the application. It receives user input from the controller. The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects.

How many types of controllers are there in IOS?

There are two types of ViewControllers: Content ViewController: Content ViewControllers are the main type of View Controllers that we create. The Content View Controllers holds the content of the Application screen.


2 Answers

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"];
    ...
}
like image 159
Till Avatar answered Nov 29 '22 17:11

Till


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.

like image 43
rhummelmose Avatar answered Nov 29 '22 16:11

rhummelmose