Firebase完成加载后,如何使功能不继续

时间:2018-06-20 15:19:50

标签: swift firebase

在我的项目中,在进入游戏场景之前,我正在从数据库中加载各种玩家。问题是我到各个地方加载不同的变量。是否可以设置某种return语句以使函数等待继续执行,直到完成每个Firebase函数的加载?这是调用各种小函数的大函数。我确定这是一个快速修复,我只是Firebase的新手。预先感谢!

 //quickevent
            loading.isHidden = false
            gameType = 3
            isMainCard = false
            let mainCardFighterInt = Int(arc4random_uniform(3)) + 1
            let undercardFighterInt = Int(arc4random_uniform(5)) + 1
            if mainCardFighterInt == 1 {
                mainCardFighter.ID = USfighter1.ID
            }
            if mainCardFighterInt == 2 {
                mainCardFighter.ID = USfighter2.ID
            }
            if mainCardFighterInt == 3 {
                mainCardFighter.ID = USfighter3.ID
            }
            loadMainCardForFight()
            //set underCard Fighter now
            if undercardFighterInt == 1 {
                underCardFighter.ID = USfighter4.ID
            }
            if undercardFighterInt == 2 {
                underCardFighter.ID = USfighter5.ID
            }
            if undercardFighterInt == 3 {
                underCardFighter.ID = USfighter6.ID
            }
            if undercardFighterInt == 4 {
                underCardFighter.ID = USfighter7.ID
            }
            if undercardFighterInt == 5 {
                underCardFighter.ID = USfighter8.ID
            }
            loadUnderCardForFight()

            //find what guy you are playing
            findMainCard = false
            let randomForUndercard = Int(arc4random_uniform(100)) + 1
            if randomForUndercard >= 95 {
                findGoldPlayerToFight()
            }
            if randomForUndercard >= 55 && randomForUndercard <= 94 {
                findSilverPlayerToFight()
            }
            if randomForUndercard <= 54 {
                findBronzePlayerToFight()
            }
            loadOpponentUnderCard()
            //find main card now
            findMainCard = true
            let randomForMainCard = Int(arc4random_uniform(100)) + 1
            if randomForMainCard >= 95 {
                findDiamondPlayerToFight()
            }
            if randomForMainCard >= 65 && randomForMainCard <= 94 {
                findElitePlayerToFight()
            }
            if randomForMainCard <= 64 {
                findGoldPlayerToFight()
            }
            loadOpponentMainCard()

            //Actually enter game
            let nextScene = GameScene(fileNamed: "GameScene")
            nextScene!.scaleMode = scaleMode
            self.view?.presentScene(nextScene)

从firebase加载的功能(如果相关)是: loadMainCardForFight,loadUnderCardForFight,findGoldPlayerToFight(青铜和其他),loadOpponentUndercard和loadOpponentMainCard。

建议后编码,新错误:加载时间不长。这是代码

 //quickevent
            loading.isHidden = false
            gameType = 3
            isMainCard = false
            let mainCardFighterInt = Int(arc4random_uniform(3)) + 1
            let undercardFighterInt = Int(arc4random_uniform(5)) + 1
            if mainCardFighterInt == 1 {
                mainCardFighter.ID = USfighter1.ID
            }
            if mainCardFighterInt == 2 {
                mainCardFighter.ID = USfighter2.ID
            }
            if mainCardFighterInt == 3 {
                mainCardFighter.ID = USfighter3.ID
            }
            loadMainCardForFight{
                //set underCard Fighter now
                if undercardFighterInt == 1 {
                    underCardFighter.ID = USfighter4.ID
                }
                if undercardFighterInt == 2 {
                    underCardFighter.ID = USfighter5.ID
                }
                if undercardFighterInt == 3 {
                    underCardFighter.ID = USfighter6.ID
                }
                if undercardFighterInt == 4 {
                    underCardFighter.ID = USfighter7.ID
                }
                if undercardFighterInt == 5 {
                    underCardFighter.ID = USfighter8.ID
                }
                self.loadUnderCardForFight{
                    //find what guy you are playing
                    findMainCard = false/*
                     let randomForUndercard = Int(arc4random_uniform(100)) + 1
                     if randomForUndercard >= 95 {
                     findGoldPlayerToFight()
                     }
                     if randomForUndercard >= 55 && randomForUndercard <= 94 {
                     findSilverPlayerToFight()
                     }
                     if randomForUndercard <= 54 {
                     findBronzePlayerToFight()
                     }*/
                    //findBronzePlayerToFight{
                    //
                    //}
                    opponentUnderCard.ID = 26
                    self.loadOpponentUnderCard{
                        //find main card now
                        findMainCard = true
                        /*let randomForMainCard = Int(arc4random_uniform(100)) + 1
                         if randomForMainCard >= 95 {
                         findDiamondPlayerToFight()
                         }
                         if randomForMainCard >= 65 && randomForMainCard <= 94 {
                         findElitePlayerToFight()
                         }
                         if randomForMainCard <= 64 {
                         findGoldPlayerToFight()
                         }*/
                        opponentMainCard.ID = 26
                        self.loadOpponentMainCard{
                            //Actually enter game
                            let nextScene = GameScene(fileNamed: "GameScene")
                            nextScene!.scaleMode = self.scaleMode
                            self.view?.presentScene(nextScene)
                        }

                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:2)

如评论中所提到的那样,是必经之路。

func loadPlayers(completion: @escaping() -> Void){
//FIRE BASE STUFF......

//done firebase

completion()
}

像这样使用它:

loadPlayers{
// This will execute until firebase is done
}