Alamofire请求未执行 [英] Alamofire Request not getting executed

查看:259
本文介绍了Alamofire请求未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生了一些奇怪的事情。我有两个ViewControllers A& B。

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

在我们两个中都使用以下命令导入Alamofire

In both of them I have imported Alamofire using below command

import Alamofire

问题:我打电话的Alamofire请求完全相同在两个控制器中。在VC-A中,它在VC-B中执行 - 只是不执行..没有错误或任何东西。当我使用断点进行调试时,由于某种原因,整个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.

以下是我的Alamofire代码(这两个控制器A和B都是相同的)

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)
}

以上代码在执行ViewController A时打印响应,但不在View Controller B中打印响应。对于viewcontroller B,其他命令只执行Alamofire而不执行。在上面的代码中 - Check 1& Check 3打印到控制台而不是Check 2(对于VC-B)。

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).

推荐答案

Alamofire代码是异步的,这意味着当你的print语句可以成功并同步执行时,viewDidLoad或viewDidAppear(或其他地方)中的某些函数可能永远循环,不允许alamofire.request执行其闭包。

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.

例如下面的代码将打印:check1,check3,但由于viewDidAppear中的func是阻塞的,Alamofire无法执行其异步代码。因此,在此示例中,Alamofire请求将不执行任何操作。如果你注释掉无限循环,代码就可以工作。

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")
}

另外我建议转移到Alamofire 3.0 https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md

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

这篇关于Alamofire请求未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆