J
J
JohnZoidberg2015-01-23 12:13:33
Objective-C
JohnZoidberg, 2015-01-23 12:13:33

Where to put JSON loading and parsing code in iOS app?

I'm learning objective C on a simple application - select a city from the list and show its current temperature in a new window.
For every window there is a ViewController. The list of cities (with temperature data cache) is stored in a plist file. The request to the weather service occurs asynchronously on the first window (to update the cache) and on the second window to get the latest data for the selected city.
There is an idea to make a class for a file with cities and temperatures, with file access methods and methods for downloading data from a weather service.
But it is not clear how then to update the Label with the temperature. Would it be correct for the second view controller to call a method of this class, wait for it to return a value, and put this value in the label text?
Now the second view controller simply sends an asynchronous request and changes the label text in the handler.
Actually the question is - where to put the code for working with the weather service?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
agee, 2015-01-23
@JohnZoidberg

When you return to the main thread after a response from the server, save the results to your plist and then send a notification about this event. NSNotificationCenter and NSNotification to the rescue.
Any objects can subscribe to this notification. So if the viewController is currently active, which displays the weather in a particular city, feel free to update the data after receiving the notification.
As for "where to put the code for working with the weather service." Considering that your application is tiny by description, you can safely "shove" this code into the root viewController's viewDidLoad. Otherwise, if you want to access the server from different places, you can create a separate singleton with a simple interface in one method a la

- (void)temperatureListWithCompletion:(void(^)(NSError * error, NSArray *results));
And call it from wherever your heart desires. However, you should not forget that it makes no sense to send a new request until the answer to the previous one is exactly the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question