我从库中运行一个我无法看到实现的函数。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
someClass.beginService();
});
这个库类调用我的类'委托函数。
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
}
}
然后在日志窗口中每隔1秒定期打印一个单词delegateCall is called!
,因为我呼叫self.someClass.beginService();
。
问题是,如何终止此线程?我不想诉诸:
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;
}
}
因为这意味着后台例程仍然有效。
答案 0 :(得分:0)
尝试使用无效功能。 喜欢
var thread = dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.someClass.beginService();
你想在哪里停止
thread.invalidate()