I run a function from a library which I can't see the implementation.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
someClass.beginService();
});
This library class calls my class' delegate function.
class Mine: UIViewController, SomeClassDelegate {
let someClass = SomeClass();
override func viewDidLoad () {
super.viewDidLoad();
someClass.delegate = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.someClass.beginService();
});
}
func delegateCall () {
print ("delegateCall is called!");
// do some stuff
}
}
The word delegateCall is called!
is then printed periodically every 1 second in the log window, because I call the self.someClass.beginService();
.
The problem is, how can I terminate this thread? I don't want to resort to:
class Mine: UIViewController, SomeClassDelegate {
let someClass = SomeClass();
var okToRun = false;
override func viewDidLoad () {
super.viewDidLoad();
someClass.delegate = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.someClass.beginService();
okToRun = true;
});
}
func delegateCall () {
if okToRun {
print ("delegateCall is called!");
// do some stuff
}
}
@IBAction func buttonStopTapped (sender: AnyObject) {
okToRun = false;
}
}
Because this means that the background routine is still working.