Search code examples
swiftalamofireswift-package-manager

Alamofire library not responding with Swift Package Manager


I am new bee with Swift Package Manager, I am creating an executable, in which I need to work on REST API, for that I am using Alamofire, this is how I have added it in my package file

  let package = Package(
name: "CreateMenuTool",
dependencies: [
    .Package(url: "https://github.com/Alamofire/Alamofire.git", majorVersion: 4)
])

Now when I create a request, I don't get any response. However, this method response me when I use it in my XCode project.

Alamofire.request(urlString, method: .post, parameters: requestParams,  encoding: JSONEncoding.default, headers: ["X-Key" : webKey]).responseJSON { response in 
   print(response)
}

When I debug it, I found it never goes inside the method which means neither I get any error nor any response. Please let me know where I am doing wrong?


Solution

  • Alamofire uses asynchronous requests. If you don't create a RunLoop, your code will exit long before the response has arrived from the server. Before the end of your script, add this line:

    RunLoop.main.run()
    

    This will start the main RunLoop. You can also pass arguments to run to have it run for a specified amount of time:

    RunLoop.main.run(until: Date(timeIntervalSinceNow: 10))