NSRunLoop cancelPerformSelectorsWithTarget's not working


I have this following code and I am not getting the results I expected.

#import "CancelPerformSelectorTestAppDelegate.h" @implementation CancelPerformSelectorTestAppDelegate @synthesize window; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [window makeKeyAndVisible]; for(unsigned int i = 0; i < 10; i++){ NSTimeInterval waitThisLong = i; [self performSelector:@selector(foo) withObject:nil afterDelay: waitThisLong]; } [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget: self]; return YES; } - (void) foo { static unsigned int timesCalled = 0; ++timesCalled; NSLog(@"%s: I am called for the %d-st/nd/th time", __func__, timesCalled); } - (void)applicationWillResignActive:(UIApplication *)application {} - (void)applicationDidBecomeActive:(UIApplication *)application {} - (void)applicationWillTerminate:(UIApplication *)application {} - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {} - (void)dealloc { [window release]; [super dealloc]; } @end

I expected the function to be called about 0 times, perhaps 1 if the CPU is having a slow day.

The function will execute 10 times! :( Always. What am I doing wrong, and how could I achieve the results I expected?

Thanks in advance, a lot, Nick


You want to cancel the request using the <a href="http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html" rel="nofollow">NSObject</a> class method +cancelPreviousPerformRequestsWithTarget:

For example,

[NSObject cancelPreviousPerformRequestsWithTarget:self];

There's an example in the "handling tap gestures" section of the <a href="http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MultitouchEvents/MultitouchEvents.html" rel="nofollow">Event Handling Guide for multitouch events</a>


You want this:

[UIApplication cancelPreviousPerformRequestsWithTarget:self];


  • Animate transform.scale with UIViewAnimationOptionBeginFromCurrentState not working as expected
  • NSRunLoopCommonModes constant in RoboVM
  • Objective-C performSelector when to use colon?
  • NSRunLoop is receiving a strange selector; possible race condition tomfoolery?
  • How to make performSelector:withObject:afterDelay work?
  • UIView animation not working in iOS 7
  • Use of NSRunloop for avoiding event driven code
  • dispatch_once conversion Swift 3
  • MBProgresshud with tableview
  • Count while in background (NSTimer for more than 3 mins)
  • CLLocation Manager not updated when App is in Guided Access mode
  • ios8 Swift SpriteKit - Pause and Resume NSTimers in swift
  • How to call JSON webservice from background in iPhone
  • Swift cannot output when using NSTimer
  • how to convert a unix timestamp into nsdate in iphone [duplicate]
  • EXC_BAD_ACCESS with NSFetchedResultsController
  • Passing argument within action selection in UIButton
  • Using ant, find files matching a regular expression and search if a substring in present in the file
  • NSTimer and updating UI
  • respondsToSelector - not working
  • Angular 2: is styleUrls relative to the current component?
  • Incorrect behaviour when selecting chips in Angular Material
  • In Angular 2 how to get @Input value updated inside component?
  • Simple Angular 2 app gives “Potentially unhandled rejection” error
  • How to click on a link that has a certain content in puppeteer?
  • How to make the tableview response pan gesture in ZUUIRevealController
  • How to replace TouchesBegan with UIGestureRecognizer
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Django model for a Postgres view
  • How to retrieve information from antrun back to maven?
  • Jquery popup on mouse over of calendar control
  • How to remove a SwiftyJSON element?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Meteor helpers not available in Angular template
  • Obtain ObjectIdHex value from mgo query
  • Build own AppleScript numerical error handling
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Bitwise OR returns boolean when one of operands is nil
  • Observable and ngFor in Angular 2
  • java string with new operator and a literal