延迟NSDocument关闭

时间:2017-09-24 08:18:23

标签: objective-c macos

我尝试安排任务或等到NSDocument关闭时任务完成执行。我已覆盖-canCloseDocumentWithDelegate方法,并希望在此处等待,直到任务完成。该任务是从后台线程生成的,但是将执行切换到主线程。

我尝试了以下

-canCloseDocumentWithDelegate
{
   if( condition )
   {
      // Run the task and wait for the task to be completed.
      scheduleTask;
      dispatch_async_on_main_queue( canCloseDocumentWithDelegate )
      return;
   }
   if( taskRunning )
   {
      // Wait until the Task completes.
      // Set a handler that'll be called on task completion.
      // Invoke canCloseDocumentWithDelegate from callback.
      callback = dispatch_async_on_main_queue( canCloseDocumentWithDelegate )
      setCompletionHandlerForTask( callback );
      return;
   }

// if the condition didn't match or the task completed continue Document Close
[super canCloseDocumentWithDelegate];
}

只要用户等待,这就有效。但是,如果用户再次单击关闭,则应用程序崩溃,因为taskRunning案例会对多个回调进行排队。

我不确定这是否是正确的方法。我想知道是否有任何委托或其他事情可以做到这一点。

0 个答案:

没有答案