Handling a button action while a timer loop is running
I have an app that checks integers one at a time until it finds one that meets a particular conditions. Since this check may take quite a bit of time, I want to be able to interrupt it with a UIButton. The method I am currently using is to create a non-repeating NSTimer with an interval of 0.001 seconds that calls an action that checks the next 100 integers, then, if none of them met the conditions, create a new NSTimer identical to the previous one and add it to currentRunLoop.
There is also a boolean variable runProcess, initially set to YES, and a UIButton labelled "Stop" that sets it to NO. In the method called when the timer is fired, if runProcess = NO, the new timer is not created. (Using an NSTimer allows for the button click to be detected in the middle of the processing.)
The NSTimer is not repeating as the processing within the called method takes longer than 1 millisecond, and I do not want messages from the timer to "stack up" so they would continue to be handled even after the processing is done and the timer no longer needs to be fired.
Is this the best way to do something like this? Would having a repeating timer (and then check to see if runProcess = YES before doing any of the processing) be better? Is using a short-period NSTimer even the way to go about this?
-- Don
|