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?
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?
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));
anObject
is unknown, usually used by first asking if the object has the method with -[NSObject respondsToSelector:]
.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.
methods are more important than classes/interfaces
is insightful, and explains a key difference between Objective-C and other OOP environments. Thanks. –
Ditheism 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.
© 2022 - 2024 — McMap. All rights reserved.
id foo; if ([foo respondsToSelector:@selector(bar)]) [foo bar];
When you do needperformSelector:
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