I have an issue on how to refresh the UI for iOS apps. What I wanted to achieve is this:
- Show data in UITableView based on data retrieved from web service
- The web service should be called from a separate thread (not main thread)
- After the data is retrieved, it will refresh the contents of UITableView with the retrieved data
- 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?