0

I have been using Alamofire in my whole project. Now in one of my modules, the Alamofire.request() is not getting executed. I tried putting a breakpoint and it showed that all of a sudden the request is being skipped. As I am working on a live project for security purpose am not including the URL. The code which I used is given here.

func responseFunction() {

    let url = ""
    let parametervalue = ["enq_id" : self.enquiry_id]

    print(self.enquiry_id)


    Alamofire.request(url, method: .post, parameters: parametervalue).responseJSON(completionHandler: {response in

       if let response_data = response.result.value as? NSDictionary{

         let rdata = response_data.value(forKey: "response") as! String

         if rdata == "Success"
         {
            let res_data = response_data.value(forKey: "result") as! [NSDictionary]
            print(res_data)
            for resultdata in res_data{

                let enqid = resultdata.value(forKey: "enq_id") as! String
                let userid = resultdata.value(forKey: "user_id") as! String
                let reference_no = resultdata.value(forKey: "reference_no") as! String
                let enq_date = resultdata.value(forKey: "enq_date") as! String
                let enq_time1 = resultdata.value(forKey: "enq_time") as! String
                let enq_time2 = enq_time1.replacingOccurrences(of: "+", with: " ")
                let enq_time = enq_time2.replacingOccurrences(of: "%3A", with: " : ")

            }

             self.queryResponseTable.reloadData()

         }

         else{

            let alertController = UIAlertController(title: "Response Details", message: "Server Error ! Could not get any response. Please try again later!", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: .default))
            self.present(alertController, animated: true, completion: nil)

         }
     }

    })

}

Can anyone help me out?

Kuldeep
  • 3,841
  • 6
  • 25
  • 50
varsha94
  • 151
  • 2
  • 16
  • You need to clarify what do you mean by "not getting executed". If the method is called, that line should be called as well. Is the method being called? Are you not receiving a response? – Adis Nov 19 '19 at 09:48
  • the line Alamofire.request() line alone is not getting executed. The line is being skipped. It's not even getting executed which means it ain't giving any response either . @Adis – varsha94 Nov 19 '19 at 10:03
  • So, your print is executed, but Alamofire is not? That seems rather impossible, I think you're identifying the issue wrongly. – Adis Nov 19 '19 at 12:35
  • Yes, the print before the Alamofire is executed. While building the project there are no errors nor any exceptions are being thrown. I still have no idea how it is not working. @Adis – varsha94 Nov 19 '19 at 14:23
  • So, if you place a breakpoint directly at that line, it just executes over it? – Adis Nov 19 '19 at 14:33
  • yes. It just executes over it @Adis – varsha94 Nov 20 '19 at 03:44
  • I tried this method [https://stackoverflow.com/questions/41997641/how-to-make-nsurlsession-post-request-in-swift] , but it works only sometimes. The first time when I executed it didn't work, the second time when it was built it worked, and now it is not working again. @Adis – varsha94 Nov 20 '19 at 04:44

1 Answers1

1

I think the below code helps you.

func Api_withParameters(aView : UIView, aWebServiceName : String, aDictParam : Dictionary<String, Any>, completeBlock: @escaping ( _ response: AnyObject) -> Void, errorBlock: @escaping ( _ error: AnyObject) -> Void) {

    let passingURL = "\(BASEURL)/\(aWebServiceName)"
    let jsonData = try? JSONSerialization.data(withJSONObject: aDictParam, options: [])
    var jsonString = String(data: jsonData!, encoding: .utf8)
    jsonString = jsonString!.filter { !"\\)(\n\t\r".contains($0) }

    let passingParameter = [K_APIKEY:APIKEY, K_Data:jsonString!]

    print("***** Web-Service URL : \(passingURL) \n Passing Parameter : \(passingParameter)")

    Alamofire.request(passingURL, method: HTTPMethod.post, parameters: passingParameter, encoding: URLEncoding.default, headers: HEADER_CONTENT_TYPE).responseJSON { (response:DataResponse<Any>) in
        switch(response.result) {
        case .success(_):
            completeBlock(response.result.value as AnyObject)
            break

        case .failure(let error):
            var errorDict = [String : Any]()
            let errorcode : Int = error._code
            if let httpStatusCode = response.response?.statusCode {
                switch(httpStatusCode) {
                case 404:
                    errorDict = ["errormsg" : "Invalid URL: \(passingURL)", "errorCode" : httpStatusCode]
                default: break

                }
            } else {
                if (String(errorcode) == "-1009"){
                    errorDict = ["errormsg" : "Please check your internet connection"]
                }
                else if(error.localizedDescription != ""){
                    errorDict = ["errormsg" : error.localizedDescription]
                }
                else{
                    errorDict = ["errormsg" : "Server unreachable please try again later."]
                }
            }
            errorBlock(errorDict as AnyObject)
            break
        }
    }
}