如何等待递归直到函数完成执行

Posted

技术标签:

【中文标题】如何等待递归直到函数完成执行【英文标题】:how to wait Recursion until the function finish executing 【发布时间】:2019-04-06 18:40:26 【问题描述】:

我有一个执行动作的递归函数,并在它的最后一个循环中检查条件,如果条件为真,它会调用另一个类似的函数,该函数也运行动作。但是,当第二个函数通过递归执行动作时,第一个函数开始执行。所以现在有两个动作同时运行。我阅读了GCD并尝试过,但没有得到预期的结果。(实际上我仍然不明白如何正确使用它)

第一个主函数 movePiece()

这是我的代码,它们都在同一个类中:

func movePiece() 
    // some Func Calls here.
    if ( self.movesRemaining > 0) 
        DispatchQueue.global(qos: .userInteractive).async 
            self.moveFinished = false

            let currentSpace:Int = self.returnPlayerSpace(player: self.whosTurn)
            let spaceNumber:Int = currentSpace
            var nextSpace:Int = spaceNumber + 1

            let moveAction:SKAction = SKAction.move(to: (self.childNode(withName: String(nextSpace))?.position)!, duration: 0.4)
            moveAction.timingMode = .easeOut

            let wait:SKAction = SKAction.wait(forDuration: 0.2)

            let runAction:SKAction = SKAction.run(
                if (self.movesRemaining == 1) 
                    self.KillHim(player:self.whosTurn, nextspace:nextSpace)
                  DispatchQueue.main.async 
                    self.moveBackPiece()
                  
                

                self.setThePlayerSpace(space: nextSpace, player:self.whosTurn)

                if (self.movesRemaining == 1) 
                    self.Adjust(node: self.childNode(withName: String(nextSpace))!)
                

                self.movesRemaining = self.movesRemaining - 1
                self.movePiece()
            )

            if self.whosTurn == .Player1 
                self.touchedNode.run(SKAction.sequence([moveAction, wait, runAction]))
             else 
                self.playerPiece.run(SKAction.sequence( [moveAction, wait, runAction]))
            
        
    

这是第二个函数 moveBackPiece() :

func moveBackPiece() 
    if ((killedPiece == self.Player1Piece1 || killedPiece == self.Player1Piece2 || killedPiece == self.Player1Piece3 || killedPiece == self.Player1Piece4) && self.movesRemainingBack != 1) || ((killedPiece == self.Player2Piece1 || killedPiece == self.Player2Piece2 || killedPiece == self.Player2Piece3 || killedPiece == self.Player2Piece4) && self.movesRemainingBack != 5) || ((killedPiece == self.Player3Piece1 || killedPiece == self.Player3Piece2 || killedPiece == self.Player3Piece3 || killedPiece == self.Player3Piece4) && self.movesRemainingBack != 9) || ((killedPiece == self.Player4Piece1 || killedPiece == self.Player4Piece2 || killedPiece == self.Player4Piece3 || killedPiece == self.Player4Piece4) && self.movesRemainingBack != 13) 
        self.movesRemainingBack -= 1

        let moveAction:SKAction = SKAction.move(to: (self.childNode(withName: String(self.movesRemainingBack))?.position)!, duration: 0.4)
        moveAction.timingMode = .easeOut

        let runAction:SKAction = SKAction.run(
            if ((self.killedPiece == self.Player1Piece1 || self.killedPiece == self.Player1Piece2 || self.killedPiece == self.Player1Piece3 || self.killedPiece == self.Player1Piece4) && self.movesRemainingBack == 1) || ((self.killedPiece == self.Player2Piece1 || self.killedPiece == self.Player2Piece2 || self.killedPiece == self.Player2Piece3 || self.killedPiece == self.Player2Piece4) && self.movesRemainingBack == 5) || ((self.killedPiece == self.Player3Piece1 || self.killedPiece == self.Player3Piece2 || self.killedPiece == self.Player3Piece3 || self.killedPiece == self.Player3Piece4) && self.movesRemainingBack == 9) || ((self.killedPiece == self.Player4Piece1 || self.killedPiece == self.Player4Piece2 || self.killedPiece == self.Player4Piece3 || self.killedPiece == self.Player4Piece4) && self.movesRemainingBack == 13) 
                self.setThePlayerSpaceBack(space: self.movesRemainingBack, Killed: self.killedPiece)
            

            self.movesRemaining = self.movesRemaining - 1
            self.moveBackPiece()
        )

        self.killedPiece.run(SKAction.sequence([moveAction, runAction]))
    

如何用 GCD 或其他方法解决它? 感谢您的宝贵时间。

【问题讨论】:

【参考方案1】:

您必须使用 GCD 组队列。它也适用于异步操作。就像进去 -> 出去,进去 -> 出去,最后它会通知你,这里是例子

    let queue = DispatchQueue(label: "com.your.text", attributes: .concurrent)
let group = DispatchGroup()

group.enter()
queue.async 
    print("1st Operation")
    Thread.sleep(forTimeInterval: 10)
    print("1st Operatio finished")
    group.leave()


group.enter()
queue.async 
    print("2nd Operation")
    Thread.sleep(forTimeInterval: 10)
    print("2nd Operation finished")
    group.leave()


group.notify(queue: .main) 
        print("Finished")
    

这里写得很快,所以可能会出现语法错误。希望对您的问题有所帮助。

【讨论】:

以上是关于如何等待递归直到函数完成执行的主要内容,如果未能解决你的问题,请参考以下文章

等待函数完成,直到回调到来

函数等待返回直到 $.getJSON 完成

创建执行动画的等待队列

等待递归 readdir 函数结束

python实现异步调用函数执行

如何在一个请求中等待,直到另一个请求完成 nodeJS 中相同功能的执行