Boon
Boon

Reputation: 41480

Does performSelector perform right away or is it scheduled to be performed?

Does performSelector perform right away or is it scheduled to be performed an iota later?

Upvotes: 1

Views: 121

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

From the doc

The performSelector: method is equivalent to sending an aSelector message directly to the receiver.

So it performs right away.

Also from the doc, these two messages are equivalent

id myClone = [anObject copy];
id myClone = [anObject performSelector:@selector(copy)];

and both of them will actually end to be compiled into

objc_msgSend(anObject, @selector(copy));

EDIT

After the discussion that originated under Anoop's answer, I think it's worth specifying that not all the variants of performSelector: are executed right away. There's a bunch of variants defined by NSObject that will cause the action to be scheduled. Moreover it's important to notice that this holds true even in case of a 0 delay, as clearly stated by the documentation:

Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible.

To wrap it up, here's a relevant list of variants

Variants with right-away execution:

  • performSelector:
  • performSelector:withObject:
  • performSelector:withObject:withObject:

Variants whose execution is scheduled (even with 0 delay)

  • performSelector:withObject:afterDelay:
  • performSelector:withObject:afterDelay:inModes:
  • performSelectorOnMainThread:withObject:waitUntilDone:
  • performSelectorOnMainThread:withObject:waitUntilDone:modes:
  • performSelector:onThread:withObject:waitUntilDone:
  • performSelector:onThread:withObject:waitUntilDone:modes:
  • performSelectorInBackground:withObject:

Upvotes: 4

Jeff Wolski
Jeff Wolski

Reputation: 6372

performSelector:, performSelector:withObject: and performSelector:withObject:withObject: all perform right away.

You can find the doc here.

Upvotes: 1

Related Questions