Threading in iPhone programming

As Kdl pointed out, it is a good point to remind the threading skeleton proposed during the Stanfor CS 192P iPhone programming course.


// LetsMakeAThreadAppDelegate.m
// LetsMakeAThread
//
// Created by Evan Doll on 10/27/08.

#import "LetsMakeAThreadAppDelegate.h"

@implementation LetsMakeAThreadAppDelegate

@synthesize window;
@synthesize spinner;
@synthesize answerLabel;
@synthesize button;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
answerLabel.text = @"";

// Override point for customization after application launch
[window makeKeyAndVisible];
}

- (IBAction)beginDeepThought:(id)sender
{
[spinner startAnimating];
button.hidden = YES;

[NSThread detachNewThreadSelector:@selector(backgroundThinking) toTarget:self withObject:nil];
}

- (void)backgroundThinking
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread sleepForTimeInterval:5];
[self performSelectorOnMainThread:@selector(didFindAnswer:) withObject:@"42" waitUntilDone:NO];
[pool release];
}

- (void)didFindAnswer:(NSString *)answer
{
[spinner stopAnimating];
answerLabel.text = answer;
}

- (void)dealloc {
[window release];
[spinner release];
[answerLabel release];
[button release];

[super dealloc];
}

@end

In his post, Kdl point out also how to set lock on variables and how to implement a cache.


- (void)someMethod
{
[myLock lock];
// We only want one thread executing this code at once
[myLock unlock]
}

- (UIImage *)cachedImageForURL:(NSURL *)url
{
id cachedObject = [cachedImages objectForKey:url];
if (cachedObject == nil) {
// Set the loading placeholder in our cache dictionary.
[cachedImages setObject:LoadingPlaceholder forKey:url];

// Create and enqueue a new image loading operation
ImageLoadingOperation *operation = [[ImageLoadingOperation alloc] initWithImageURL:url target:self action:@selector(didFinishLoadingImageWithResult:)];
[operationQueue addOperation:operation];
[operation release];
} else if (![cachedObject isKindOfClass:[UIImage class]]) {
// We're already loading the image. Don't kick off another request.
cachedObject = nil;
}

return cachedObject;
}

- (void)didFinishLoadingImageWithResult:(NSDictionary *)result
{
NSURL *url = [result objectForKey:@"url"];
UIImage *image = [result objectForKey:@"image"];

// Store the image in our cache.
// One way to enhance this application further would be to purge images that haven't been used lately,
// or to purge aggressively in response to a memory warning.
[cachedImages setObject:image forKey:url];

[self.tableView reloadData];
}


// Package it up to send back to our target.
NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:image, ImageResultKey, imageURL, URLResultKey, nil];
[target performSelectorOnMainThread:action withObject:result waitUntilDone:NO];

P.S.: Sorry KDL, I actually copied your code, but your code and pre tags didn’t look good on my browser!

This entry was posted in Programming and tagged , , , . Bookmark the permalink.

One Response to Threading in iPhone programming

  1. Pingback: Frankie

Leave a Reply