2

Possible Duplicate:
How does -performSelector:withObject:afterDelay: work?

I often use this code to let the UI finish its business before calling a long-running method:

[obj performSelector:@selector(go) withObject:nil afterDelay:0];

But what does it do?

My personal interpretation has always been that the go method is called on the next run loop, but surely that's not even right.

Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421

1 Answers1

5

Calling it with delay 0 will indeed invoke this method on the next pass through the runloop.

IIRC, what it does is set up a struct that represents the target and action, and attach a CFRunLoopSource to the runloop that, when triggered, will invoke the action on the target. It then signals the runloop to tell it that it has a ready source. This means that the next time the runloop processes its sources (i.e. the next pass through the runloop), it will perform your delayed selector.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347