Wyler
Wyler

Reputation: 77

Completion Block seemingly being ignored

So I have a function with a completion block that handles animation and a function calling that function. During the function call the completion block is seemingly skipped over because no print lines are printed. I have tried putting the code to be executed into the main queue but it has not yielded any results. Here is how I'm calling the function:

runAnimations(past.count, currentIteration: 0, animationsArray: past, completion: {
        finished in
        var randomr = self.randomInRange(0,hi: 3)
        println("sell")
        past.append(randomr)
        for i in past123{
            if(past.count >= 10){
            }
            else{
                // have tried with and without dispatch_async.. neither print little did I know
                dispatch_async(dispatch_get_main_queue()) {
                    println("little did I know")
                    self.incrementGame(past)
                }
            }
        }

    })

I know it is not an issue with the past.count if statement because the sell print line is not executed either. A breakpoint at the function call simply shows it skipping over however I am aware this is due to it being in an async thread.

Here is the code for runAnimations function:

func runAnimations(numberToIterate:Int, currentIteration:Int, animationsArray : [Int], completion: ((Bool) -> Void)?) {
    UIView.animateWithDuration(1, animations: {
            if(animationsArray[currentIteration] == 0){
                self.label.text="circle!"
                //self.label.alpha = 0.0
            }
            else if(animationsArray[currentIteration] == 1){
                self.label.text="square!"
                //self.label.alpha = 0.0
            }
            else if(animationsArray[currentIteration] == 2){
                self.label.text="triangle!"
                //self.label.alpha = 0.0
            }
            else if(animationsArray[currentIteration] == 3){
                self.label.text="star!"
                //self.label.alpha = 0.0
            }
        }, completion: {
            finished in
            println("BRO")
            if(currentIteration+1>=numberToIterate){
                self.label.alpha = 1.0
                println("ook")
            }
            else{
                println("okies")
                self.runAnimations(numberToIterate, currentIteration: currentIteration+1, animationsArray: animationsArray, completion: { finished in

                    })
                //self.runAnimations(numberToIterate, currentIteration: currentIteration+1, animationsArray: animationsArray)
            }
    })

The prints in runAnimation execute properly.

Upvotes: 0

Views: 86

Answers (1)

Nate Lee
Nate Lee

Reputation: 2842

You actually have to call the completion block, which you haven't done by doing

completion(finished)

The completion block you defined can be seen as a separate method, which you need to call. In your case you have left your block as an optional so you must do optional checking as per the standard (completion != nil, etc)

This is a very "swift" guide on a completion block in swift, which is now called a closure. Take a look: here

Upvotes: 1

Related Questions