1

Something strange is happening. I have two ViewControllers A & B.

In both of them I have imported Alamofire using below command

import Alamofire

ISSUE: I am calling exactly the same Alamofire Request in both controller. In VC - A it executes, in VC - B --its just not executing..There is no error or anything. When I debug using breakpoints, the entire Alamofire code is getting skipped for some reason. Can't seem to figure out why.

Below is my Alamofire Code (THIS IS SAME in BOTH Controllers A & B)

override func viewDidLoad() {
    super.viewDidLoad()
print("check1") 
Alamofire.request(.GET, hubsURL).response { request, response, result, error in
        print(response)
        print("check2")
}
print("check"3)
}

The above code prints the response when ViewController A is executed but not for View Controller B. For viewcontroller B other commands are getting executed only Alamofire not getting executed.In the above code - "Check 1" & "Check 3" get printed to console but not "Check 2" (for VC - B).

Anuj Arora
  • 3,017
  • 3
  • 29
  • 43
  • Can you add the code surrounding the Alamofire request call in VC B? It's hard to tell what is going on without more context – chimpymike Oct 08 '15 at 18:59

2 Answers2

1

Alamofire code is asynchronous, that means while your print statements may be executed successfully and synchronously, some function in either viewDidLoad or viewDidAppear (or somewhere else) may be looping forever not allowing alamofire.request to execute its closure.

For example this code below will print: check1, check3, but because the func inside viewDidAppear is blocking, Alamofire can't execute its asynchronous code. So in this example the Alamofire request will do nothing. If you comment out the endless loop, the code will work.

override func viewDidLoad() {
    super.viewDidLoad()
    print("check1") 
    Alamofire.request(.GET, hubsURL).response { request, response, result, error in
        print(response)
        print("check2")
    }
    print("check"3)
}

override func viewDidAppear(animated: Bool) {
    while (true != nil) {
    }
    print("After While")
}

Plus I would suggest moving to Alamofire 3.0 https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md

Bndr
  • 317
  • 5
  • 13
0

There can be many problems, we cannot tell if you do not provide the entire code. Maybe you just messed with other part of the code, for example

  • Is that action triggered by a button? Is the button/action properly connected in your code/storyboard?
  • Did you set correctly the UIViewController in the Storyboard? Meaning is the viewcontroller you are calling a VC - B? (Is your viewDidLoad method called?)

And many more possibilities, but if it works for VC - A there shouldn't be a problem with Alamofire. If you still cannot solve consider editing your question with additional code.

r4id4
  • 5,877
  • 8
  • 46
  • 76
  • The view controllers have been set properly. In View Controller B -- there are other commands which are running. The alamofire requests have been made in viewDidLoad methods of A & B – Anuj Arora Oct 08 '15 at 23:34
  • Try adding `print("log")` (or `println("log")` for Swift 1.2) before you call `Alamofire.request`. Is it printed? – r4id4 Oct 09 '15 at 07:28
  • Yes..I have tried before & after..those print commands get printed..I have edited my question to reflect that. – Anuj Arora Oct 09 '15 at 08:18
  • Is it the exact same `Alamofire` piece of code you use for VC - A? Can you try commenting the code in VC - A and see if it works for VC - B then? – r4id4 Oct 09 '15 at 08:23
  • Tried that..still no luck..the weird part is that I implemented the same code in another ViewController C just to check and it worked fine...its just not working in VC - B – Anuj Arora Oct 09 '15 at 08:30
  • There must be an error with VC-B that is not regarding `Alamofire`, otherwise it wouldn't work for VC-C too. Is you VC -C code equal to VC-B? – r4id4 Oct 09 '15 at 09:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91827/discussion-between-anuj-arora-and-moonie). – Anuj Arora Oct 09 '15 at 10:07