iosobjective-cnsurlconnectionnsxmlparserviewcontroller

What is BEST way to download data from server and show in UITableViewController or UIViewController without stuck of its GUI?


There are many structures to download data from server. like

  1. ViewDidLoad Make NSURLConnection, call its delegate and when connection ends, make parser and parser data and then save to DB. (EVERYTHING IN THE SAME CLASS)

Another way is

  1. Make a separate class, that will manage all NSURLConnections + NSXMLParser, and just make object of that class in ViewDidLoad or any other method of ViewController Class.

Another way is

  1. Make a file to set connection, and make another file to manage parser

     ViewController =====================>> URLConnection
     Delegate of URLConnectionfile =====================>> ViewController 
    

then, in this delegate, NSData which is downloaded from server is further send to Parser class

   ViewController =======================>> ParserClass
  Delegate of Parser gives Array to ================= ViewController

then display that Array

Another way is to use thread

  1. Call such methods in

    [self performSelectorInBackground:@selector(doSomething) withObject:nil];

then in doSomething , call your connection file

Can anyone define the best way to download and save in a very organised and proper way, so that it becomes easy to manage and no hang of View occur.


Solution

  • Don't operate your downloading and parsing code on main thread. That is the only way to avoid UI freezes.

    Recommended ways,

    1. NSOperation and NSOperationQueue
    2. GCD
    3. Use 3rd party frameworks like AFNetworking, MKNetworkKit etc.

    Also, if you have images in your table view, use technique called lazy loading.

    I'd recommend not to use [self performSelectorInBackground:@selector(doSomething) withObject:nil];. It gets messy if you don't know how to use, rather use GCD.


    EDIT

    Usually, what I follow,

    Call to WS is initiated in viewDidLoad, with progress indicator shown until process completes. But some times requirement is that the view controller is not created until WS response is downloaded. So choice to initiate request is based on your requirement.