Communication between model and controller - iOS

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

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

I implement two views (utility-based application using storyboards). In the main view, I use a text box and a search button, where the user can enter a query, and then click the search button. After clicking the search button, my intention is to read the value of the text field and use it in my callback to my web service. My web service responds using a JSON file with the results of a query that I parse and display in the secondary view area.

I know how to make a calm call in iOS and how to do JSON parsing and also display the results on the screen (at least the text, but this is another question). But my intention is to learn and implement the basics of MVC for my application.

In accordance with MVC, the controller updates the view, and the model sends a notification that the controller can listen to and know if there are any changes in the object. So this is what I would ideally like:

My model. My model will handle the main RESTful call, receive a JSON response, parse it and get the resulting values ​​that I want to display in the view.

My Controller - I want my controller to listen to my model and get the resulting values ​​from the model and display them in the view.

Using a quick and dirty way, I can implement a RESTful call, parse JSON and display the resulting values ​​- everything is inside the controller, but using this technique, if my view changes tomorrow, then I will have to rewrite my code. Or, if I want to add new features, I have to change my controller. Therefore, ideally, I would like to have a basic model that does not know what the view looks like, and just let the controller take the results from the model and display them in the view.

From what I read from Google's search results, there are still two ways to do this: a) Monitoring key values ​​and b) Notification Center.

Over the past 2 days, I have been trying to find a good decent way to implement the Notification Center or learn more about it, I do not get a good result. Some of the questions I have, can I send the value of a String result using the Notification Center that my controller receives? How does the notification center really work with string values? Where can I find good examples?

Therefore, any help in this regard would be greatly appreciated.

+6
source share
2 answers

Some of the questions that I have, can I send the results to a String value using the notification center that my controller receives?

Yes, this is usually done using the userInfo property for NSNotification . userInfo is a simple NSDictionary that can contain instances of NSObject derived objects indexed by keys that adhere to the NSCopying protocol (commonly used by NSString ). Note that the dictionary ( userInfo ) will save your / s option.


How does the Notification Center work with string values?

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


Where can I find good examples?

Perhaps this helps ...


Example

Register of the receiver (controller) for 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 a notification in its handler:

 - (void)modelObjectUpdatedString:(NSNotification *)notification { ModelObject *postingObject = [notification object]; NSString *string = [[notification userInfo] objectForKey:@"String"]; ... } 
+12
source

You are thinking the right way, but still not completely. Because Till “points” in its comment, you should not process the RESTful message inside your model. If I were you, I would create a utility class that is responsible for fetching information, and then a class that is responsible for storing data (this last class is your model).

It would be wise to create a class method that allocates and initiates a new instance of this object, created from JSON data obtained using your RESTful communicator class.

From the point of view of your controller:

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

You can take advantage of CoreData here, and I highly recommend that you explore this.

+4
source

Source: https://habr.com/ru/post/913277/


All Articles