我正计划使用Xcode中的Web服务调用,并且需要它具有高性能。我是否要从后台线程调用服务,然后在另一个线程上更新UI?
答案 0 :(得分:1)
我认为这应该适合您的目的(用 Swift 编写):
// Go to an asynchronous thread
// .userInitiated quality of service for a high-priority task
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
// Make sure you have a reference to self if you need it here
guard let self = self else {
return
}
// Make your asynchronous web service call here
// Now return to the main thread
DispatchQueue.main.async { [weak self] in
// Make sure you have a reference to self if you need it here
guard let self = self else {
return
}
// Update your UI here
}
}
有关DispatchQueue
和服务质量的更多信息,我推荐此tutorial。