如何在使用并发时不让你的应用程序在退出时崩溃

How to not crash your app on quit when using concurrency

我正在使用 NSOperationQueueNSOperation 的子 class 作为我应用程序中生成大量数据的部分,因此计算量很大。

当应用程序被用户关闭时 processingQueue.cancelAllOperations() 被调用。同样在我的 NSOperation subclass 中,我重写了 cancel() 以让它将取消请求转发给执行实际繁重工作的 class ...

override func cancel() {
    AppDelegate.instance.cancelDataGeneration()
    super.cancel()
}

但这还不够。当我在数据生成正在进行时关闭应用程序时,它会在 Xcode.

中崩溃

如何防止崩溃(这可能会导致数据丢失)?让应用程序等待关闭直到所有并发操作都被取消是否可以,这是如何完成的(如果可能的话)?或者通常使用什么其他方法来解决这个问题?


更新:

经过更多调查,我发现我的 NSOperation 子 class 上的 cancel() 从未被调用,即使在 applicationShouldTerminate 中调用 processingQueue.cancelAllOperations() 之后也是如此。所以我添加了一个手动调用取消的方法:

func cancelDataGeneration() {
    if let op = AppDelegate.instance._operation {
        op.cancel();
    }
}

我从 applicationShouldTerminate 内部调用它(因为 applicationShouldTerminateapplicationWillTerminate 更早被调用。有趣的是,由于我的 AppDelegate 是单例,所以我必须使用 AppDelegate.instance._operation . 如果我只检查 _operation,当从 applicationShouldTerminate 调用时,结果是 nil。知道为什么会这样会很有趣。

在任何情况下,取消现在都可以正常工作:当应用程序退出时,它将取消数据生成 class 并退出而不会崩溃......无论如何。但我仍然想知道为什么我的 NSOperation subclass' cancel() 在我使用 processingQueue.cancelAllOperations()!

时没有被调用

来自 Apple's documentation.

Canceling the operations does not automatically remove them from the queue or stop those that are currently executing. For operations that are queued and waiting execution, the queue must still attempt to execute the operation before recognizing that it is canceled and moving it to the finished state.

我会阻塞 App 的主线程,直到 NSOperationQueue 完成它的所有工作。

  • 我会先打电话给 [NSOperationQueue cancelAllOperations]
  • 然后在 'Application will terminate' 方法中调用 [NSOperationQueue waitUntilAllOperationsAreFinished]。这将确保当前正在执行的块(所有其他排队的任务将被取消)将在应用程序退出之前完成。

    现在,如果在当前执行的块完成之前您对主线程 blocking 不满意,那么您需要检查一个标志(或者 NSApplicationDelegate 可以设置在 class) 表示应用程序是否仍处于活动状态以便继续。如果要终止应用程序,那么块 voluntarilyfall out,这是最干净的方法。

    大致如下所示。

    void ^(aBlock)(void) = ^void(void)
    {
        for(NSUInteger i = 0;i < 1000; i++)
        {
            // heavy processing code. Taking several iterations each second
            // At the start of each iteration, check for a flag, to see if to quit
            if(_applicationIsShuttingDown) break;
    
            // perform block operation
        }
    };
    

    并且您的 class 是一个 NSApplicationDelegate 并实现了

    -applicationWillTerminate:(NSNotification *)aNotification
    {
        _applicationIsShuttingDown = YES;
    }