Foobar
Foobar

Reputation: 8487

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?

Upvotes: 1

Views: 61

Answers (1)

Marco Santarossa
Marco Santarossa

Reputation: 4066

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.

Upvotes: 0

Related Questions