我正在尝试处理来自服务器的响应,并且收到了响应,但是,需要一段时间才能继续其余部分。令人惊讶的是,打印内容工作正常,我检查实时服务器更改,它也工作正常.所以这是xcode造成的。
收到回复并检查是否成功后,我检查..
// Received HTTP Response
if let status = json["status"].string {
if status == "success" {
print("C")
activityView.stopAnimating()
activityView.removeFromSuperview()
self.someAnimation()
print("D")
} else {
...
}
}
令人惊讶的是,C & D 会立即打印出来,但事件动画似乎并没有在中间停止。大约需要 10 秒才能完成、停止并删除 ActivityIndicator。自定义动画 someAnimation()
也与 ActivityIndicator 同时继续。
我知道这是一个异步线程,但它不是必须停止 ActivityIndicator 并在到达 print("D")
时继续吗?
最佳答案
我认为您正在后台线程上执行 UI 任务,这会导致未定义的行为。避免这种情况的一种方法是将 UI 方法调用包装在 dispatch_async
中:
// Received HTTP Response
if let status = json["status"].string {
if status == "success" {
print("C")
dispatch_async(dispatch_get_main_queue(), {
activityView.stopAnimating()
activityView.removeFromSuperview()
self.someAnimation()
});
print("D")
} else {
...
}
}
关于ios - 函数在收到响应后需要一段时间才能继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36618486/