iOS applications containing long running tasks - such as HTTP requests or data processing - that block the main thread are annoying and often appear sluggish. The following snippet can be used to fix this issue:
//
// SampleTableViewController.m
// Sample
//
// Created by Kevin Sylvestre on 10-04-22.
// Copyright Kevin Sylvestre 2010. All rights reserved.
//
#import "SampleTableViewController.h"
@implementation SampleTableViewController
#pragma mark - Helpers
- (void)load
{
[NSThread sleepForTimeInterval:4.0];
self.results = [NSArray arrayWithObjects:@"Canada", @"England", @"France", @"Spain", nil];
[self.view performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
#pragma mark - Main
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
[queue addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(load) object:nil] autorelease]];
}
@end