Threading in Objective-C Threading in Objective-C multithreading multithreading

Threading in Objective-C


An easy way to just spin off a method in a new thread is to use.

+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument on NSThread. If you aren't running garbage collected you need to set up your own autorelease pool.

Another easy way if you just don't want to block the main thread is to use.

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg on NSObject

Depending on what type of concurrency you are after you should also take a look at NSOperation that can give you free locking so you can share it between several threads among other things.


If you're developing using Cocoa (ie for the mac or iphone), you have access to the NSThread class, which can be used for multithreading. Googling for NSThread will find you the API.

You can declare it like using:

NSThread *mythread = [[NSThread alloc] initWithTarget:target selector:selector object:argument];

Where target and selector is the object and selector you want to start a thread with, and argument is an argument to send to the selector.

Then use [mythread start] to start it.


Yes there are threading concepts in Objective C. and there are multiple way to achieve multi threading in objective C.

1> NSThread

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

This will create a new thread in the background. from your main thread.

2> Using performSelector

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

will perform UI task on your main thread if you call this from background thread... You can also use

[self performSelectorInBackground:@selector(abc:) withObject:obj];

Which will create a background thread.

3> Using NSoperation

Follow this link

4> Using GCD

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        [self callWebService];        dispatch_async(dispatch_get_main_queue(), ^{            [self updateUI];        });    });

Will callWebService in background thread and once it's completed. It will updateUI in main thread.More about GCD

This is almost all way of multithreading that is used in iOS. hope this helps.