目前我使用此方法获取当前视图控制器:
func topMostContoller()-> UIViewController?{
if !Thread.current.isMainThread{
logError(message: "ACCESSING TOP MOST CONTROLLER OUTSIDE OF MAIN THREAD")
return nil
}
let topMostVC:UIViewController = UIApplication.shared.keyWindow!.rootViewController!
return topVCWithRootVC(topMostVC)
}
此方法遍历从rootViewController开始的视图控制器的层次结构,直到它到达顶部。
func topVCWithRootVC(_ rootVC:UIViewController)->UIViewController?{
if rootVC is UITabBarController{
let tabBarController:UITabBarController = rootVC as! UITabBarController
if let selectVC = tabBarController.selectedViewController{
return topVCWithRootVC(selectVC)
}else{
return nil
}
}else if rootVC.presentedViewController != nil{
if let presentedViewController = rootVC.presentedViewController! as UIViewController!{
return topVCWithRootVC(presentedViewController)
}else{
return nil
}
} else {
return rootVC
}
}
此问题位于topMostController
,因为它使用UIApplication.shared.keyWindow
和UIApplication.shared.keyWindow.rootViewController
,不应在后台线程中使用。我收到了这些警告:
runtime: UI API called from background thread: UIWindow.rootViewController must be used from main thread only
runtime: UI API called from background thread: UIApplication.keyWindow must be used from main thread only
所以我的问题是。是否有线程安全的方式来访问当前显示的视图控制器?
答案 0 :(得分:0)
从主线程访问是否符合您的需求?
func getTopThreadSafe(completion: @escaping(UIViewController?) -> Void) {
DispatchQueue.main.async {
let topMostVC: UIViewController = UIApplication.shared.keyWindow?.rootViewController?
completion(topMostVC)
}
}
这可能会有点混乱,因为它是一种异步方法,但我的直觉告诉我,无论你做什么,这都是最安全的选择:)