-1

I have an iOS application where I get my info from the servers, the problem is that none of the requests I am using is working because the code execution is exiting the code, before getting the response back from the server, or maybe it is failing, anyways here is what I have tried so far:

let manager = AFHTTPSessionManager()

        manager.post("www.myapps.com/api/Files/getFiles?folderId=6", parameters: nil, progress: nil, success: { (task: URLSessionTask, responseObject) in
            if let response = responseObject as? [String:Any],
                let medias = response["medias"] as? [String:Any] {

                locked = false
                let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

                let url      = NSURL(fileURLWithPath: documentsPath)
                let contentFile = url.appendingPathComponent("Content.json")
                if let data = try? JSONSerialization.data(withJSONObject: medias) {
                    try? data.write(to: contentFile!, options: .atomic)
                }

                let data = try! Data(contentsOf: contentFile!)
                self.jsonData =  JSON(data:data)
                self.categoriesCount = self.jsonData["categories"].count
                print(self.categoriesCount)
            }
        },failure: { (operation: URLSessionTask!, NSError) in
            print(NSError.localizedDescription)
        })
    while locked {
        wait()
    }
}

func wait()
{
    DispatchQueue.main.async {
       RunLoop.current.run(mode: RunLoopMode.defaultRunLoopMode, before: NSDate(timeIntervalSinceNow: 1) as Date)
    }

}

keep in mind that if I use my URL directly in my browser I do get a response which means it is working, but in my application, something is going wrong any ideas?

user3411226
  • 183
  • 1
  • 14
  • 1
    What exactly is wrong with the code you posted? Perhaps you need to show the full function this code is in. – rmaddy Feb 12 '18 at 17:08
  • the problem is that this is always failing, maybe due to the execution of the code after this while i still have no response, or maybe something is wrong with post method I am using. – user3411226 Feb 12 '18 at 17:09
  • Failing how? You need to provide useful details in your question. Are there errors? Put them in your question. Is the code simply not executing the way you expect? Then clearly describe in what way it is incorrect. – rmaddy Feb 12 '18 at 17:11
  • i have edited my question and i am getting no error here the debugger is not going in the success nor the failure block, it is just jumping to the wait method and keeps looping. – user3411226 Feb 12 '18 at 17:18
  • 3
    **Do not wait**. Use a completion handler [Here](https://stackoverflow.com/questions/39643334/how-do-you-write-a-completion-handler-in-swift-3/39643395#39643395) is one of countless examples. – vadian Feb 12 '18 at 17:20
  • 1
    Don't post synchronously. Don't `wait`. Do things correctly. – matt Feb 12 '18 at 17:29
  • the problem is that the code is still running before even getting the response from the server, so functions that depend on this response's data are giving errors. – user3411226 Feb 14 '18 at 13:43

1 Answers1

-1

i solved the following by using Alamofire synchronous method as follows:

let response = Alamofire.request("http://www.appsandgamesinc.com/api/Files/getFiles", method: .post, parameters: ["folderId": "6"]).responseJSON(options: .allowFragments)
        if let json = response.result.value {
            print(json)
}
user3411226
  • 183
  • 1
  • 14