Top of the page
Close

This is a red error

This is a red error

This is a red error

Example of threading in iPhone SDK (Objective-C using NSThread)

Ondrej Rafaj on 2009.09.04 17:38:18

Example how to use create a new thread using NSThread to run some method in the background of your application.
- (IBAction) startThreadButtonPressed:(UIButton *)sender {
	
	[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
	
}

- (void)startTheBackgroundJob {
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
	[NSThread sleepForTimeInterval:3];
    [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
    [pool release];
	
}

- (void)makeMyProgressBarMoving {
    
	float actual = [threadProgressView progress];
	if (actual < 1) {
		threadProgressView.progress = actual + 0.01;
		[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(makeMyProgressBarMoving) userInfo:nil repeats:NO];
	}
	
}
Update: Full tutorial about threading with NSThread and sample application is here

Ondrej Rafaj

Ondrej Rafaj

Independent iPhone developer @ ondrej-rafaj.co.uk

I am available to give you a free quote or start working on your project ... just give me a call or drop a line. Please find all my details on my portfolio site ondrej-rafaj.co.uk

Back to top Comment

Comments ... Why not to get involved!

We had to disable comments till the problem with spam will be solved, sorry for any inconvenience caused.

Back to top

IE SUCKS, DON'T USE IT :) Digg this page