Raju
Raju

Reputation: 3469

Threading in Objective-C

Are there threads in Objective C? If so, how are they declared and used?

If anybody knows about multithreading in Objective C, please share with me.

Thanks and regards.

Upvotes: 10

Views: 22202

Answers (5)

Mihir Mehta
Mihir Mehta

Reputation: 13833

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.

Upvotes: 10

James Williams
James Williams

Reputation: 1891

Before going to far with stuff like detachNewThreadSelector: be sure to check out Apple's official documentation. For a high-level overview of options (including operation queues, dispatch queues, and such), there's the Concurrency Programming Guide. And, for a look at lower-level (and less recommended) threading, there's the Threading Programming Guide.

You definitely don't want to just start spinning up threads without reading what Apple has to say on the subject first. They've done a lot of work with stuff like GCD to make it easier and safer to write concurrent programs.

Upvotes: 2

Tom Jefferys
Tom Jefferys

Reputation: 13310

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.

Upvotes: 11

Abizern
Abizern

Reputation: 150565

You could also look into NSOperation

To see an example of this, have a look at Drew McCormack's post on MacResearch.

Upvotes: 3

monowerker
monowerker

Reputation: 2979

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.

Upvotes: 18

Related Questions