I want to terminate all currently called methods and then simply re-init my current object. So when the user clicks on say the Start button, I call a bunch of methods...start doing stuff. Then when the user clicks on Cancel, i need to simply stop all running tasks/methods and re-init.
I don't want the app itself to terminate. So cannot use [NSApp terminate:nil]. I just need all current functions to stop and then i can call [self init]; Thanks in advance.
First, you have to set some flag and disable your user interface until all methods are completed. For example, self.cancelled = YES
.
Next, each of your methods should be adjusted to return if that flag is set:
- (void)doSomething
{
if (self.cancelled) return;
// ...
for (/* ... */) {
// ...
if (self.cancelled) return;
// ...
}
// etc...
}
Then, you need a mechanism to track all your methods. Most likely, you will use NSOperationQueue
or dispatch_queue_t
. Take a look into documentation to find out how to get a notification when the queue becomes empty.
Finally, reset the self.cancelled
flag and unblock your user interface.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.