Kevin Sylvestre

A Ruby and iOS developer and designer living in Santa Monica, California.
A Ruby and iOS developer and designer living in Santa Monica, California.
  • About
  • Contact
  • rss
  • archive
  • Non-Blocking Long Running Tasks on iPhone

    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
    
    • April 22, 2010 (1:53 pm)
    • #apple
    • #cocoa
    • #ipad
    • #iphone
    • #mac
© 2009–2013 Kevin Sylvestre