Dispatchgroup似乎并没有持有其他事件,有什么事情发生?

时间:2019-01-31 19:59:41

标签: swift function firebase return dispatch

所以我建立这个函数getQuestionValues(),以提取和返回信息的阵列拿来火力。和它的作品,直到某一点。 功能被正确地从火力获取数据并将其存储到一个数组。我知道,因为当我打印阵列刚刚追加的最后一个项目,它返回预期的结果之后。

override func viewDidLoad() {
    super.viewDidLoad()

    if let nav = self.navigationController {
        nav.isToolbarHidden = false
    }
    nextPath = sortNextQuestion()
    getQuestionValues(statementPath: nextPath[0], alternativeAPath: nextPath[1], alternativeBPath: nextPath[2], alternativeCPath: nextPath[3], alternativeDPath: nextPath[4], correctPath: nextPath[5])


    dispatchGroup.notify(queue: .main){
        print("nextquestion is gonna be:")
        print(self.nextQuestion)

    }
}

功能我建立:

 func getQuestionValues(statementPath: String, alternativeAPath: String, alternativeBPath: String, alternativeCPath: String, alternativeDPath: String, correctPath: String){
    print("entering dispatch group")
    self.dispatchGroup.enter()

    let refStatement = Database.database().reference(withPath: statementPath)
    let refOptionA = Database.database().reference(withPath: alternativeAPath)
    let refOptionB = Database.database().reference(withPath: alternativeBPath)
    let refOptionC = Database.database().reference(withPath: alternativeCPath)
    let refOptionD = Database.database().reference(withPath: alternativeDPath)
    let refCorrect = Database.database().reference(withPath: correctPath)

    refStatement.observe(.value) { (snapshot) in
        print(snapshot)
        let statement = snapshot.value as? String
        self.questionFetched.append(statement!)


    }
        //fetching alternativeA
        refOptionA.observe(.value) { (snapshot) in
            let optionA = snapshot.value as? String
            self.questionFetched.append(optionA!)


        }

        //fetching alternativeB
        refOptionB.observe(.value) { (snapshot) in
            let optionB = snapshot.value as? String
            self.questionFetched.append(optionB!)

        }

        //fetching alternativeC
        refOptionC.observe(.value) { (snapshot) in
            let optionC = snapshot.value as? String
            self.questionFetched.append(optionC!)


        }

        //fetching alternativeD
        refOptionD.observe(.value) { (snapshot) in
            let optionD = snapshot.value as? String
            self.questionFetched.append(optionD!)


        }

        //fetching correctAnswer
        refCorrect.observe(.value) { (snapshot) in
            let correct = snapshot.value as? Int
            let correctString = String(correct!)
            self.questionFetched.append(correctString)
            //left this print here just to understand that the function actually works, it is just delayed
            print(self.questionFetched)
        }



        print("new question fetched inside function:")
        print(self.questionFetched)

    print("leaving dispatch")
    self.dispatchGroup.leave()




}

不过,我没有得到我想要的预期时间的结果。我想用在viewDidLoad()其结果,但似乎代码内的块dispatchGroup.notify(queue: .main)不等待功能getQuestionValues()至负载,正如我意欲。

会发生什么?

0 个答案:

没有答案