在 Swift 中判断是否在主线程

/ Mac / 没有评论 / 2269浏览

在 Swift 中判断是否在主线程

func getState() -> UIApplication.State {
  if Thread.isMainThread {
    return UIApplication.shared.applicationState
  } else {
    var state: UIApplication.State?
    DispatchQueue.main.async {
      state = UIApplication.shared.applicationState
    }
    return state!
  }
}