简体   繁体   English

迅速完成进度

[英]Implementing Completion Block swift

I have implemented completion block which has a logic error. 我实现了具有逻辑错误的完成块。 I want when the checkOutBtn is clicked checkFields is triggered first to check if all the text fields is not empty before it triggers the addingDeliveryAddress() method to insert into the database before performing the sesueway. 我想在单击checkOutBtn时首先触发checkFields,以检查所有文本字段是否都不为空,然后再触发执行添加方式之前,它触发addingDeliveryAddress()方法插入数据库。 But its not working like that when checkOutBtn is clicked it goes ahead and perform the segueway. 但是,当单击checkOutBtn时,它无法正常工作,并继续执行segueway。 Thanks all for your help. 感谢你的帮助。 Thanks 谢谢

   @IBAction func checkOutBtn(_ sender: Any) {

    checkFields { (results) in
        if results {
            self.addingDeliveryAddress()
        }
    }
}


func checkFields(_ completion: @escaping (Bool) -> ()){
        if (recipientName.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Name"
            completion(false)
        }else if (recipientMobile.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Mobile Number"
            completion(false)
        }else if (recipientArea.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Area"
            completion(false)
        }else if (recipientAddress.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Address"
            completion(false)
        }
        completion(true)
    }



    //Adding Delivery Address
    func addingDeliveryAddress(){

        //getting user data from defaults
        let defaultValues = UserDefaults.standard
        let userId = defaultValues.string(forKey: "userid")

        //creating parameters for the post request
        let parameters: Parameters=[
            "recipientName":recipientName.text!,
            "recipientPhoneNumber":recipientMobile.text!,
            "recipientArea":recipientArea.text!,
            "recipientAddress":recipientAddress.text!,
            "nearestLandmark":recipientLandmark.text!,
            "userId":Int(userId!)!
        ]

        //Constant that holds the URL for web service
        let URL_ADD_DELIVERY_ADDRESS = "http://localhost:8888/restaurant/addDeliveryAddress.php?"

        Alamofire.request(URL_ADD_DELIVERY_ADDRESS, method: .post, parameters: parameters).responseJSON {
            response in
            //printing response
            print(response)

            let result = response.result.value

            //converting it as NSDictionary
            let jsonData = result as! NSDictionary

            //if there is no error
            if(!(jsonData.value(forKey: "error") as! Bool)){

                self.performSegue(withIdentifier: "toCheckOut", sender: self)

            }else{

                let alert = UIAlertController(title: "No Delivery Address", message: "Enter Delivery Address to continue", preferredStyle: .alert)

                alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: nil))
                //alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

                self.present(alert, animated: true)
            }
        }
    }

Why a completion block? 为什么要完成一个区块? There is no asynchronous process. 没有异步过程。

I suggest this way which returns (directly) the error string or an empty string on success. 我建议以这种方式成功返回(直接)错误字符串或空字符串。

@IBAction func checkOutBtn(_ sender: Any) {

    let result = checkFields()
    if result.isEmpty {
        self.addingDeliveryAddress()
    } else {
       errorMessageLbl.textColor = UIColor.red
       errorMessageLbl.text = "Enter Recipient " + result
    }
}

func checkFields() -> String {
    if recipientName.text!.isEmpty {
        return "Name"
    } else if recipientMobile.text!.isEmpty {
        return "Mobile Number"
    } else if recipientArea.text!.isEmpty {
        return "Area"
    } else if recipientAddress.text!.isEmpty {
        return "Address"
    }
    return ""
}

In your code you're using @escaping in closure. 在您的代码中,您使用@escaping闭包。 It's wrong as you're not doing anything asynchronous in this closure body. 这是错误的,因为您没有在此闭包主体中执行任何异步操作。 When using @escaping the closure is being preserve to be execute later and function's body gets executed. 当使用@escaping时 ,闭包将被保留以在以后执行,并且函数的主体将被执行。 That's why addingDeliveryAddress() gets triggered before checking anything. 这就是为什么在检查任何内容之前会触发addingDeliveryAddress() Your closure function should be @nonescaping like this.. 你的闭包函数应该像这样@nonescaping

func checkFields(_ completion: (Bool) -> ()){
        if (recipientName.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Name"
            completion(false)
        }else if (recipientMobile.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Mobile Number"
            completion(false)
        }else if (recipientArea.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Area"
            completion(false)
        }else if (recipientAddress.text?.isEmpty)! {
            errorMessageLbl.textColor = UIColor.red
            errorMessageLbl.text = "Enter Recipient Address"
            completion(false)
        }
        completion(true)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM