What does the performSelector method do?
Asked Answered
S

2

9

What does performSelector do? What is the difference between creating a new NSThread and the performSelector method?

How it works and where should we use it?

Spragens answered 30/11, 2010 at 12:43 Comment(0)
I
23

All of these perform the same task, that is make the doStuff method on anObject execute synchronously on the current thread:

// 1
[anObject doStuff];

// 2
[anObject performSelector:@selector(doStuff)];

// 3
objc_msgSend(anObject, @selector(doStuff));

// 4
IMP imp = [anObject methodForSelector:@selector(doStuff)];
imp(anObject, @selector(doStuff));
  1. Is how you normally should go about to do stuff.
  2. Is for dynamically dispatching a message. Use if the selector is unknown, or provided by a client, for example if you implement an target-action pattern. or if the class of anObject is unknown, usually used by first asking if the object has the method with -[NSObject respondsToSelector:].
  3. Is what no 1. is actually compiled down to. Usually never any real need to do this.
  4. Cached the actual IMP (implementation) for a method, and then call it directly. Can sometimes be faster than 1. if used in a tight loop. Just remember; premature optimization is evil.

What you need to grasp is that in Objective-C methods are more important than classes/interfaces. Usually you do not query an object if it belongs to a particular class, or conforms to any protocol, that is for the compiler to complain about. At run-time you instead query for specific methods.

In short: It does not matter what you are, just what you can do.

As a convenience NSObject also have several siblings to performSelector that are asynchronios. Most notably:

  • performSelector:withObject:afterDelay: - To execute the method on the current thread after a delay.
  • performSelectorInBackground:withObject: - To execute the method on a new background thread.
  • performSelectorOnMainThread:withObject:waitUntilDone: - To execute the method on the main thread.
  • performSelector:onThread:withObject:waitUntilDone: - To execute the method on any thread.

The asynchronous performers all depend on a NSRunLoop to function. This is not something you need to worry about unless you spawn a thread yourself. If you do then you need to also run the new threads run loop. Just skip that for now.

Issiah answered 30/11, 2010 at 15:48 Comment(3)
You can use the direct method call when you don't know the class of an object -- id foo; if ([foo respondsToSelector:@selector(bar)]) [foo bar]; When you do need performSelector: is if you don't know the selector to call at compile-time (e.g. it's sent in from a different object, or it's user-selectable, etc).Piranesi
(3) is sometimes useful because (2) only works with methods whose arguments are all objects and whose return type is an object or void, and which only has 0-2 arguments. For anything else you can use (3)Wyon
That comment about methods are more important than classes/interfaces is insightful, and explains a key difference between Objective-C and other OOP environments. Thanks.Ditheism
D
2

performSelector executes a selector. In other words, it calls a method.

It is very different from running a new thread.

I think it would be best for you to read up on selectors.

Diadiabase answered 30/11, 2010 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.