Alamofire请求未执行

编程入门 行业动态 更新时间:2024-10-22 19:44:02
本文介绍了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 github/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md

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

更多推荐

Alamofire请求未执行

本文发布于:2023-11-26 03:06:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!