使用Xcode调用异步Web服务

时间:2018-11-27 23:54:05

标签: xcode multithreading web asynchronous service

我正计划使用Xcode中的Web服务调用,并且需要它具有高性能。我是否要从后台线程调用服务,然后在另一个线程上更新UI?

1 个答案:

答案 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