Best practice for web services data management?

Hi guys,


I have built an app that I've been working on for a little while. I basically started learning Objective-C while programming it so I have made some ***** misconception errors, mainly in the whole web service part.


Right now, I have about 10 views that all require data from a web service (an API that returns the data in jSON). The required data is slightly different from page to page. Currently, I call all the method straight from the views themselves -- this leads to a lot of code duplication, mainly in verifying if data is nil, and so on.


I know that I need to centralize the whole web service part in classes, but I'm a bit lost at how I'm supposed to do this. Where should I start? What should the structure look like?

Right now I use NSUrlConnection to make an asynchronous request. I then use NSJSONSerialization to parse my data, and I basically just store it in NSMutableArrays or an NSDictionnary.


I'm pretty sure there are some tools out there that could even do this whole management for me? Any recommendations?


I would really, really appreciate your help. Thanks a lot!

Replies

My general advice on this front is to separate your networking code from your view controllers by way of model objects. That is:

  • Have your view controllers display a set of model objects, reacting to changes in the model objects as they occur.

  • Have your network code change those model objects as network requests complete.

This has a bunch of advantages, the most important being that you can write and test your view controller code without actually doing any networking and vice versa.

To make this idea more concrete, let’s imagine an app that needs to display a photo gallery fetched from the network. When the user taps an a gallery you’d push the gallery view controller and, as part of that, give it a reference to some gallery model object. That model object has a list of photos in the gallery, where each photo has a thumbnail. Initially that list is empty, so the gallery view controller displays a loading screen. The gallery view controller would also observe the gallery for changes, updating its UI as the gallery model object changes.

At the same time you push the gallery view controller you also tell the networking code it’s time to update the gallery. It would go off to the the network to download the gallery index. Once it has that, it can update the list of photos in the gallery model object. It can then start fetching photo thumbnails, which also get put into the corresponding model objects. As these changes happen, they trigger anyone observing the gallery (like the gallery view controller discussed above) to update their UI.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi Eskimo,


thanks a lot for taking the time to answer my question.

That definitely makes sense. In fact, it's exactly what I want, and the general flow sounds great.


Then comes the "stupid" question...how exactly is this achieved? I mean, I don't want any code given straight to me, but I'm not familiar at all with the concept of model objects. Can you recommend any good reads about it?


Once again thanks a lot.

The official Apple docs for this is the Model-View-Controller section of the Concepts in Objective-C Programming document. OTOH my favourite Apple documentation about app architecture is a sequence of talks given by Ken Kocienda at WWDC, including:

  • WWDC 2010 Session 116 Model-View-Controller for iPhone OS

  • WWDC 2011 Session 112 Writing Easy-To-Change Code

  • WWDC 2012 Session 212 Basics+Habits: Building Your Software Projects To Last

  • WWDC 2014 Session 237 A Strategy for Great Work

I haven’t included direct links because some of them are only available in the archive. Start at the main video page and navigate from there.

Finally, this approach (and variants on it, like MVVM) have been discussed at length in third-party resources. I don’t have any specific favourite examples, but perhaps others will share.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"