3

If I do this in a subclass of UIView:

[self performSelector:@selector(doSomething) withObject:nil afterDelay:5];

Then cancel it like this (I've tried both versions):

 [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
 //[[NSRunLoop mainRunLoop] cancelPerformSelectorsWithTarget:self];

The "doSomething" method still gets called. What am I doing wrong?

Keavon
  • 6,837
  • 9
  • 51
  • 79
sol
  • 6,402
  • 13
  • 47
  • 57
  • 1
    +1 i think i noticed this recently too; i couldn't get it to work so i ended up using a non-repeating `NSTimer` instead. :P – Dave DeLong Oct 19 '10 at 17:56
  • But this isn't necessarily related to an NSTimer. It's just canceling a request... – sol Oct 19 '10 at 18:03
  • Are you sure both methods run on the same thread ? Because if they don't then they target different run loops. – DarkDust Oct 19 '10 at 18:06
  • @sol I know; I'm saying I couldn't get my `cancelPerformSelector` working either, so I went with a non-repeating timer that i could `invalidate` when I needed. – Dave DeLong Oct 19 '10 at 18:18
  • @Dave - I see. Not a bad workaround. Might have to do that myself. – sol Oct 19 '10 at 18:35
  • @DarkDust - There should be no reason that more than one thread is involved here. Is there an easy way to verify that? – sol Oct 19 '10 at 18:37

1 Answers1

1

In the NSObject class reference:

cancelPreviousPerformRequestsWithTarget:selector:object:

Cancels perform requests previously registered with performSelector:withObject:afterDelay:.

Use:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(doSomething) object:nil];

Hope this helps.

Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144