0

I have an issue on how to refresh the UI for iOS apps. What I wanted to achieve is this:

  1. Show data in UITableView based on data retrieved from web service
  2. The web service should be called from a separate thread (not main thread)
  3. After the data is retrieved, it will refresh the contents of UITableView with the retrieved data
  4. It is due so that the UI will not hang or the app will not block user input while in the process of receiving data from the web service in bad network connection

To do that, I create the following source code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *myURL = [[NSURL alloc] initWithString:[Constant webserviceURL]];
    NSURLRequest *request = [NSURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData                        timeoutInterval:60];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    [self myparser] = [[MyXMLParser alloc] initXMLParser];

    [parser setDelegate:myparser];
    BOOL success = [parser parse];
    if (success) {
        // show XML data to UITableView
        [_tableView performSelectorOnMainThread:@selector(reloadData) withObject:[myparser xmldata] waitUntilDone:NO];
    }
    else {
        NSLog(@"Error parsing XML from web service");
    }
}

================== Is my implementation correct? Anybody know how to resolve it?

1 Answers1

0

You would want to call

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

It will make the call to get the Data on a different thread then when the data pulled down or it had problems download data from the url it will call your handler block on the same thread as the original call was made.

Here is one way to use it: https://stackoverflow.com/a/9409737/1540822

You can also use

- (id)initWithRequest:(NSURLRequest *)request delegate:(id < NSURLConnectionDelegate >)delegate

And this will call one of your NSURLConnectionDelegate methods when data is downloaded in chucks. If you going to have large data then you may want to use this so that you don't spend too much time in the response.

Community
  • 1
  • 1
Pareshkumar
  • 176
  • 6
  • Hi there, thanks for responding. But do you know how the other thread (not main thread) access the UITableView object and refresh the UI itself? – ekychandra Jul 23 '12 at 07:22
  • the tableview has set of indexes it needs to update and it needs to resolve the indexes the user maybe view on the screen, it has to wait for the cells to go off screen or or update indexes for those cell on the screen. you will want to call [self performSelector:@selector(doReloadTableData) withObject:NULL afterDelay:0.0]; – Pareshkumar Jul 23 '12 at 11:45
  • Or you can use – performSelector:onThread:withObject:waitUntilDone: and hand it the MainThread for the onThread: parm. the first option you will need wrap in another call or just do a performSelection on main when you get you data. ------- like your doing now: [_tableView performSelectorOnMainThread:@selector(reloadData) withObject:[myparser xmldata] waitUntilDone:NO]; – Pareshkumar Jul 23 '12 at 11:57