I have to stop and restart a simple method that takes from 1 to 2 seconds to execute. How can this be accomplished? I have already tried [NSObject cancelPreviousPerformRequestsWithTarget:self] but it works only with performselector after delay. I have also tried to create a new thread but it doesn’t seem to work…
This is my method:
-(IBAction)MyMethod
{
NSLog(@"start");
//Here is the code that takes time to execute. It regards UI intervention,graphic calculation, x and y position etc.
NSLog(@"end");
}
I want this effect: one click on the linked UIButton and the method start (so print start log and end log). If I click the linked UIButton before the NSLog is printed, the method must stop. Is this possible?
You’ll want to use a background task. I would suggest subclassing and using a NSOperation and checking for
isCancelledwithin the body ofmain. See Apple’s documentation on using NSOperation and NSOperationQueue.