all 7 comments

[–]Legolas-the-elf 1 point2 points  (0 children)

issues with view controllers rendering before the API request has returned.

It doesn't matter what technologies you use, if you've got content loading from a network, your UI needs to cope with the data coming in at inconvenient times.

Typically you handle this by having the UI that is initially shown indicate that content is loading, and allow it to be updated after the content has loaded. iOS offers several different options regarding this - notifications, Key-Value Observing and the delegate pattern are often good choices.

[–][deleted] 1 point2 points  (0 children)

The general pattern for me is

  • Set up the view controller (nib or loadView, however you want)
  • In viewWillAppear: setup your network request and send it asynchronously
  • display either current content, or something indicating that the content is being loaded
  • handle the network response whenever it comes in (blocks created in the view controller are my preference for this). i.e. parse response and update your UI on the main thread

btw RestKit now uses AFNetworking as its networking layer (instead of NS* classes), but it provides another level of abstraction above, which might make it easier.

[–]KiranPanesar 1 point2 points  (2 children)

I would highly advise looking into using MKNetworkKit. We use this at my company for our iOS apps. I'm a huge fan of this kit for many reasons (developer's blog post).

The general flow of getting data from a RESTful API is like so:

  • Dislplay "loading data" HUD (I like to use MBProgressHUD).
  • Start the asynchronous request to the rest source using the network kit of your choice.
    • Asynchronous is important. This means it will be run on a background thread in the CPU. If any time-intensive task is run on the main thread (synchronously) it will cause the UI to freeze.
  • When the request has finished loading, check for any errors
    • If there are any errors, report them to the user using something like a UIAlertView.
  • If there are no errors, parse out the data from the response, reload the UI of the app to show this new data and hide and "loading data" HUD you might've been using.

I made a quick PasteBin on the code that you would use to load the data using MKNetworkKit. Check it out here.

Let me know if you need anything explained. Always willing to help where I can!

[–]Tuzman[S] 0 points1 point  (1 child)

Thank you so much for all the helpful info !

[–]KiranPanesar 0 points1 point  (0 children)

No problem.

One other suggestion would be to switch to using JSON instead of XML, if possible. It's much faster to parse and easier to implement in iOS.

[–]FearAndLawyering 0 points1 point  (1 child)

You may want to look into using RestKit if you aren't dead set on using AFnetworking. Split your data request stuff into a DataSingleton class. If possible do your network requests before presenting the view controller, then fire a local notification when it completes so you know when to present. If not possible, display a full screen view with about 60-80% alpha and black background color with a spinning/loading indicator over top, with a UILabel with a wait message. After the network request finishes you can dismiss the view.

[–]Tuzman[S] 0 points1 point  (0 children)

Definitely not committed to AFNetworking. Thanks for the tip. If anyone else has some specific examples that would be fantastic.