1

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?

Foobar
  • 7,458
  • 16
  • 81
  • 161

1 Answers1

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.

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
  • This does not seem to work. The console still prints "Deletion End" before the UI actually changes. – Foobar Sep 02 '16 at 23:10