简体   繁体   中英

IOS get notified when UI updates.

I have a custom keyboard extension. This function is called when the delete key is pressed:

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

I do not think the dispatch_async is relevent but I included it, just incase.

The problem is that even though my console prints "Deletion End" once the loop is finished, the UI of the textfield does not update until a second or two has passed.

It seems calling

(self.textDocumentProxy as UIKeyInput).deleteBackward()

Does not immediately delete a character and update the UI.

How can I be notified when the UI is actually updated?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM