1

我从一个我看不到实现的库中运行一个函数。

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
    }
}

delegateCall is called!然后在日志窗口中每隔 1 秒定期打印一次该单词,因为我将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;
    }
}

因为这意味着后台程序仍在工作。

4

1 回答 1

0

尝试使功能无效。喜欢

var  thread = dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            self.someClass.beginService();

你想停在哪里

thread.invalidate()
于 2016-10-21T09:23:52.913 回答