Understanding Alamofire (examples/documentation)

Hey everyone!

I have some problems getting code to run and think that it has to do with my understanding of how Alamofire and/or Swift works. I am fairly new to Swift but have been programming in other languages for years now so terminology and concepts should (hopefully) be familiar to me.

Now to my problem. I was playing around and tried to create a minimal and rough API client but am failing at the simplest steps it seems. Here is my sample code:

import Foundation
import Alamofire

public struct Client {
  private let baseUrl: String = "https://httpbin.org/get"

  public func getTest() {
    AF.request(baseUrl).response { response in
import XCTest
@testable import Dolphin

final class DolphinTests: XCTestCase {
  func testExample() throws {
    let client = Client()

So basically I just want to make a simple GET request and print out the response like in the documentation but I never get an output in my tests. Is this an async call and the test finishes before the response can be read? What is the best practice in this case anyways? At that point I am just trying to get a minimal request working and mess around with the response so any help is appreciated.

Yes, you haven’t handled the async nature of the request. You either need to connect the completion handler in your method or switch to the async APIs instead, which are simpler.

Ok perfect. So I guess the best practice is to switch to the async APIs anyways?

If you can, yes. It’s much easier to use if your app is set up for it.

Perfect, I was just confused by the documentation then because I thought I can just copy the example and then play around with it. Thank you for the help!

You can, you just have to know how to use completion handlers in Swift. The async API will now do that handling for you, another reason to switch.

Well, then I have to read up on completion handlers it seems :smiley: