Aug 4, 2008

(Objective-C) How to use multi-threading in Objective-C

To create a new thread:
[NSThread detachNewThreadSelector:@selector(threadSelector) toTarget:self withObject:nil];

in the threadSelector method we add what we want the thread to do, but don't forget to create a new NSAutoreleasePool object to manage all the autoreleased objects in the thread.

+ (void) threadSelector {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//Add what you want the thread to do

[NSThread exit];
[pool release];
}

No comments: