使用完成处理程序按顺序执行操作

时间:2019-01-04 06:22:18

标签: ios swift function firebase completionhandler

我需要按以下顺序运行三个函数:

  1. 从Firebase收集整数,将它们放入数组中,然后计算总和。完成后...
  2. 收集与Firebase不同的整数值。完成后...
  3. 第三个函数从函数2获取值,然后从函数1减去它。

我知道我需要使用完成处理程序,但是在语法等方面需要一些帮助。

///第一个函数从Firebase获取整数并将其求和:

func LoadPointsCompleted(completion: @escaping(_ sumOfPointsCompleted:Int) -> Int){

    self.challengeList.removeAll()

    databaseReference = Database.database().reference()

    let userID = Auth.auth().currentUser?.uid

    let refChallenges = Database.database().reference(withPath: "Challenges").child(userID!).queryOrdered(byChild: "Status").queryEqual(toValue: "Complete")

    refChallenges.observeSingleEvent(of: .value, with: { (snapshot) in

        //if the reference have some values
        if snapshot.childrenCount > 0 {

            //clearing the list
            self.challengeList.removeAll()



            //iterating through all the values
            for Challenges in snapshot.children.allObjects as! [DataSnapshot] {
                //getting values
                let challengeObject = Challenges.value as? [String: AnyObject]
                let Points = challengeObject?["Points"] as! Int

                //creating challenge object with model and fetched values
                let challenge = pointsModel(Points: (Points as Int?)!)

                //appending it to list
                self.challengeList.append(challenge)


                let sumOfPointsCompleted = self.challengeList.reduce(0) {$0 + $1.Points}

                let sumOfPointsCompletedString = String(sumOfPointsCompleted)
                self.Calc_Earned.text = sumOfPointsCompletedString

                completion(sumOfPointsCompleted)

            }

        }

    }

    )}

///第二个功能只是从Firebase的另一个位置获取一个整数值

func loadPointsRedeemed(completion: @escaping (_ Points_Redeem: Int) -> Int) {


    databaseReference = Database.database().reference()

    let userID = Auth.auth().currentUser?.uid

    databaseReference.child("Users").child(userID!).observe(DataEventType.value, with: { (snapshot) in

        let value = snapshot.value as? NSDictionary

        // let Points_Earn = value?["Points_Earned"] as? String ?? ""
        let Points_Redeem = value?["Points_Redeemed"] as! Int


        // self.Points_Earned.text = Points_Earn

        let points_redeemedString = String(Points_Redeem)
        self.Points_Redeemed.text = points_redeemedString

        // let pointsRedeemedAs = Points_Redeem

        completion(Points_Redeem)

        // Do any additional setup after loading the view.
    }

)}

//这第三个函数只是从前两个函数中获取值并将其相减

 func BalanceOfPoints(){

    let a = LoadPointsCompleted()
    let b = loadPointsRedeemed()


    let balance = a - b
    let balanceString = String(balance)

    self.Points_Balance.text = balanceString

}

但是,在第三个函数“ BalanceOfPoints”中出现如下错误:

在调用中缺少参数'completion'的参数-插入'completion:<(Int)-> Int>'

我不知道我第一次按以下方式启动函数时语法是否正确:

func LoadPointsCompleted(completion: @escaping(_ sumOfPointsCompleted:Int) -> Int){

和...

func loadPointsRedeemed(完成情况:@转义(_ Points_Redeem:Int)-> Int){

有人可以帮忙吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

docker-compose run image bash -c "cd /path/to/somewhere && python a.py"以闭包作为参数(完成处理程序)

您可以给它一个LoadPointsCompleted完成处理程序

nil

或给它一个处理程序

LoadPointsCompleted(completion: nil)

但是您在LoadPointsCompleted() { sumOfPointsCompleted in return <#some Int#> } 中的逻辑似乎有点错,因为BalanceOfPointsLoadPointsCompleted同时被依次调用,并且实际上并没有返回loadPointsRedeemed函数的完成处理程序会执行。例如,Int可以更改为completion(sumOfPointsCompleted),但请确保那不是您想要的。

我将这两个功能的签名更改为

let a = completion(sumOfPointsCompleted)

func LoadPointsCompleted(completion: @escaping(_ sumOfPointsCompleted:Int) -> Void){

然后在func loadPointsRedeemed(completion: @escaping (_ Points_Redeem: Int) -> Void) { 中代替

BalanceOfPoints

但是请注意,func BalanceOfPoints(){ LoadPointsCompleted() { sumOfPointsCompleted in loadPointsRedeemed() { Points_Redeem in let balance = sumOfPointsCompleted - Points_Redeem let balanceString = String(balance) self.Points_Balance.text = balanceString } } } 现在是一个异步函数。

不确定您的问题中的步骤1和步骤2是否可以并行执行,但是看起来可以并行执行,但是按照此答案中的顺序,它们是按顺序排列的