关闭顺序未按我预期的方式运行

时间:2018-11-17 20:50:50

标签: ios swift multithreading firebase asynchronous

我在firebase中有两个不同的数据类,需要将它们组合成一个类以用于我的应用程序中的某个供稿。因此,我使用闭包进行了逐步操作,以从数据库中获取信息,对其进行操作并组合成一个数组。我获取所有数据的函数运行良好,但是我需要先调用该函数,然后发生x,y和z。.问题是即使我尝试使用完成处理程序,也首先调用了错误的函数。如果我做错了请帮忙。预先谢谢你。

这里是需要首先运行以从Firebase获取所有数据的功能:

func getUserLeagues(_ completion: ()->()) {

    userLeagues.getGolferLeagues {
        print("Step 1")
        for league in self.userLeagues.leagues {
            print("FOR IN League ID: \(league.leagueID)")
            self.getLeagueInfo(leagueID: league.leagueID, completion: {
                print("Step 2")

            })
        }
        print("League Info Code Run")

    }
    completion()
}


//Get league Info
func getLeagueInfo(leagueID: String, completion: @escaping ()->()) {
    REF_leagueInfo.child(leagueID).observe(.value, with: {(snapshot) in
        if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
            self.leagueInfo.removeAll()
            for snap in snapshot {
                print("SNAP: \(snap)")

                if let leagueDict = snap.value as? Dictionary<String, Any> {
                    let key = snap.key
                    let league = LeagueInfo(leagueID: key, leagueDict: leagueDict)
                    self.leagueInfo.append(league)
                }
            }

        }
        completion()
    })
}

}

这是在加载VC时调用的函数,需要先调用第一个函数,然后才能运行其他任何东西。

func getSummaryLeaderboard(completion: @escaping ()->()) {
    self.summaryLeaderBoards.removeAll()
    print("Get Summary Leaderboard called")

    leagueInfo.getUserLeagues {
        print("Step 3")
        print("League Count: \(self.userLeagues.leagues.count)")
        for league in self.userLeagues.leagues {


            print("League ID: \(league.leagueID)")
            self.leagueInfo.getLeagueInfo(leagueID: league.leagueID, completion: {
                let leagueInfoIndex = self.leagueInfo.leagueInfo[0]
                self.leagueEndDate = leagueInfoIndex.endDate
                print("league end date: \(self.leagueEndDate)")
                self.leagueImg = leagueInfoIndex.leagueImage
                print("league img: \(self.leagueImg)")
                self.leagueName = leagueInfoIndex.leagueName
                print("league Name: \(self.leagueName)")
                self.minRounds = leagueInfoIndex.leagueMinRounds
                print("league min rounds: \(self.minRounds)")
                self.leagueLeaderScore = leagueInfoIndex.leaderPardieScore
                print("league leader score: \(self.leagueLeaderScore)")
                print("League info run")

            })


            self.leagueMembers.getLeagueMembers(leagueID: league.leagueID, completion: {
                print("GET LEAGUE MEMBERS RUN")
                self.roundsRemaining = self.fillInRoundsRemaing(minRounds: self.minRounds, roundsPlayed: self.leagueMembers.getUserRounds(userID: currentUserKey))
                print("user rounds remaining: \(self.roundsRemaining)")
                self.userPosition = self.getCurrentUserPosition(userID: currentUserKey)
                print("user position: \(self.userPosition)")
                self.roundsPlayed = self.leagueMembers.getUserRounds(userID: currentUserKey)
                print("user rounds played: \(self.roundsPlayed)")
                self.totalMembers = self.leagueMembers.leagueMembers.count
                print("user total members: \(self.totalMembers)")
                self.userWinnings = self.getCurrentUserWinnings(userID: currentUserKey)
                print("USER WINNINGS: \(self.userWinnings)")
                self.userPardieScore = self.leagueMembers.getUserPardieScore(userID: currentUserKey)
                print("user pardie Score: \(self.userPardieScore)")

                let newSummaryLeaderboard = SummaryLeaderboard(leagueEndDate: self.leagueEndDate, roundsRemaining: self.roundsRemaining, leagueImg: self.leagueImg, leagueName: self.leagueName, userPosition: self.userPosition, leagueLeaderScore: self.leagueLeaderScore, userWinnings: self.userWinnings, roundsPlayed: self.roundsPlayed, totalMembers: self.totalMembers, userPardieScore: self.userPardieScore)
                self.summaryLeaderBoards.append(newSummaryLeaderboard)
                completion()
            })

        }
    }

您会看到我说第1步,第2步和第3步的注释...显然,我需要它们运行1 2 3 ...现在它们运行3,1,2,我不知道为什么..

再次感谢!几天来这一直让我发疯。

0 个答案:

没有答案