Threading in Objective-C

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

If anyone knows about multithreading in Objective-C, share with me.

Thank you and welcome.

+6
multithreading objective-c cocoa
source share
5 answers

Using an easy way is to simply cancel the method in a new thread.

+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument on NSThread . If you are not using garbage collection, you need to configure your own autorun.

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

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

Depending on what type of concurrency you need, you can also take a look at NSOperation , which can give you a free lock so you can share it between multiple threads, among other things.

+18
source share

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

You can declare it using:

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

If the target and selector is the object and selector for which you want to start the stream with, and the argument is the argument to send to the selector.

Then use [mythread start] to start it.

+9
source share

Yes, Objective C. has thread concepts, and there are many ways to achieve multithreading in an object C.

1> NSThread

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

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

2> Using the performSelector function

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

will do the user interface task in the main thread if you call it from the background thread ... You can also use

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

What will create the 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 there be a callWebService in the background thread and after it finishes. It will updateUI in the main thread. More on GCD

This is almost the entire multithreading path that is used in iOS. hope this helps.

+5
source share

You can also watch NSOperation

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

+3
source share

Before you go far ahead, for example, detachNewThreadSelector: be sure to check out the official Apple documentation. For a high-level overview of parameters (including operation queues, send queues, etc.), Concurrency Programming Guide . And, looking at lower (and less recommended) threads, Thread Programming Guide .

You definitely don't want to just start rolling out threads without reading what Apple has to say about this in the first place. They did a great job with things like GCD to make it easier and safer to write parallel programs.

+2
source share

All Articles