ios - 在执行 AFNetwork 请求之前执行 UI 更改

标签 ios swift asynchronous afnetworking

我在登录 View Controller 中有一个 UIButton,当按下它时,从 UITextField 获取值并通过 AFNetwork 向服务器执行 POST 请求。我想在按下按钮后立即隐藏文本字段并显示 UIActivityIndi​​catorView,以便用户看到正在发生的事情。

问题是,请求是异步的,甚至在我更新 UI 之前就发生了。所以请帮我找到一种方法来实现所需的行为

pwdTextField.hidden = true

// Start the activity indicator
activityIndicator.startAnimating()    

// How can I make this happen before the actual request?    

let token = pwdTextField.text.trim()
let requestURL = "https://myapi.com/authenticate/"

manager.POST(requestURL, parameters: [ "code" : token ], success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
       NSLog("Success! Response is \(responseObject.description)")
}, failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
       println("Failure! Error is: \(error.localizedDescription)")
       self.displayLoginAttempErrorAlert()
}).waitUntilFinished()

最佳答案

删除 waitUntilFinished().. 它会阻止当前线程的执行,直到操作对象完成其任务..

manager.POST(requestURL, parameters: [ "code" : token ], success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
   NSLog("Success! Response is \(responseObject.description)")
 }, 
 failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
   println("Failure! Error is: \(error.localizedDescription)")
   self.displayLoginAttempErrorAlert()
 })

关于ios - 在执行 AFNetwork 请求之前执行 UI 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29910814/

相关文章:

ios - iPhone 设备 4s、5、5s 和 6 的 Today 小部件中未检测到设备方向

ios - SceneKit – NSAttributedString 未显示为 SCNText

javascript - 在函数中使用 fetch() 导致 "output not defined"

ios - 一段时间后 dispatch_queue_t 变慢

ios - 检测 IOS 10(Swift 3、Xcode 8)中的 GSM 调用状态和来自后台状态的通知

iphone - ScrollView 图像向左滚动后是否从内存中删除?

ios - 如何通过 SceneKit 和模型 I/O 在 .OBJ 3d 模型上应用 .MTL 文件

IOS swift 如何在标签点击时在 TableView 中获取标签文本

iphone - 异步纹理加载 iPhone OpenGL ES 2

android - 如何返回Future <object>并将其分配给object,flutter