Emilio Hoffmann
Emilio Hoffmann

Reputation: 304

Alamofire called twice

I'm trying to use Alamofire but it`s really weird. I'm calling this function and debugging, it's being called twice, I don't know why.

In the first time it just jump all the function without doing anything and the second time runs normaly.

override func viewDidLoad() {
    super.viewDidLoad()

    download{
        //do stuffs
    }

}

func download(completed: @escaping DownloadComplete){

    Alamofire.request("https://httpbin.org/get").responseJSON { response in
        print(response.request ?? "")  // original URL request
        print(response.response ?? "") // HTTP URL response
        print(response.data ?? "")     // server data
        print(response.result)   // result of response serialization

        if let JSON = response.result.value {
            print("JSON: \(JSON)")
        }
        completed()
    }
}

This ViewController it's being called by a PerformSegue WithIdentifier. I don't know what could be.

Upvotes: 2

Views: 2215

Answers (3)

Douglas Drouillard
Douglas Drouillard

Reputation: 785

It could be the debug statements itself causing the extra request. For example if you do something like this to see what the request will look like then it will actually make the request

let test = session.request(url);

print("Debug print request")
// this will make first call even though response is not handled
debugPrint(test)

// second call
session.request(url).responseJson....

Upvotes: 2

dilaver
dilaver

Reputation: 684

I am assuming you are putting a break point on the request line. When you do this its gonna trigger twice. First time when request is sended and second time when you get the response. Compiler sees the completion block as 1 line and triggers on the same line again.

Upvotes: 1

Kamajabu
Kamajabu

Reputation: 506

Check on your storyboard (Connection Inspector, ⌘ + ⌥ + 6) if there are no duplicated old connections.

Upvotes: 0

Related Questions