更新UI而不阻止用户在Swift中进行用户交互

时间:2016-03-18 11:17:37

标签: swift macos grand-central-dispatch

我实现了一个基于XCode设计的IDE。这是一个很多学习。

我需要更新UI而不阻止它。

我已经有了一对线程 使用Grand Central Dispatch。 基本上我有一个解释器,一个带有codeView(NSTextView)和一个outputView(NSTextView)的UI。当我按播放/运行时,IDE运行代码。它启动两个线程,都带有while循环。一个用于运行程序,另一个用于从解释器发送的任何输出或消息更新UI。

这样的事情:

// program execution thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
        {
            self.interpreter_s.execute()
        });
    // update UI while interpreter thread is running
    dispatch_async(dispatch_get_main_queue(),
        {
            self.updateUIWhileInterpreterRunningThread()
        });
}

它们都与另一个进行通信,因此它们具有并发性。

根据我所做的研究,建议使用main_queue()更新UI。但是,当我这样做时,它会阻止UI接收用户输入。

在我的情况下,会导致停止按钮变得无用。

所以我需要一种方法来更新来自解释器线程的更新的UI,而不会阻止用户输入的视图。 有什么想法吗?

我非常喜欢已经使用Grand Central Dispatch的解决方案。我还想避免从解释器线程调度UI更新,因为在这种特殊情况下这可能非常低效。

0 个答案:

没有答案