1

我有一个自定义键盘扩展。按下删除键时调用此函数:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
            {
                for _ in 1..<50
                {
                    (self.textDocumentProxy as UIKeyInput).deleteBackward()
                }
                print("Deletion End")
                self.deleteCounter = 0
        })

我认为这dispatch_async无关紧要,但我将其包括在内,以防万一。

问题是,即使我的控制台在循环完成后打印“删除结束”,文本字段的 UI 也不会更新,直到一两秒过去。

似乎在召唤

(self.textDocumentProxy as UIKeyInput).deleteBackward()

不会立即删除角色并更新 UI。

当 UI 实际更新时如何通知我?

4

1 回答 1

0

Change like this:

dispatch_async(dispatch_get_main_queue(),{
                for _ in 1..<50
                {
                    (self.textDocumentProxy as UIKeyInput).deleteBackward()
                }
                print("Deletion End")
                self.deleteCounter = 0
        })

Explanation:

The UI must work in the Main thread so when you work with the background queue you always have to dispatch in main queue the UI updates.

于 2016-09-02T17:27:00.993 回答