user1079052 user1079052 - 1 year ago 46
Swift Question

method not waiting until running again

I have a delegate method that is called multiple times from a framework and I need it to wait to display all letters before it is ran again. I have tried

, but another instance of the method runs when it is called again.

var myInt = 1

for letter in message.characters {

let delay = 0.1 * Double(NSEC_PER_SEC) * Double(myInt)
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

dispatch_after(dispatchTime, dispatch_get_main_queue(), {
self.textView.text = self.textView.text + String(letter)

myInt = myInt + 1
self.textView.text = self.textView.text + "\n"

Answer Source

create a serial queue

let delegateQueue = dispatch_queue_create("com.domain.delegateQueue", DISPATCH_QUEUE_SERIAL)

in the delegate method

dispatch_async(delegateQueue, ^{
    //all the things done in delegate method