注销用户会使应用程序崩溃
Logging user out crashes the app
在我的 SettingsVC 中,我发送了一个 NSNotification.Name(rawValue: "LogOut")
的通知,AppDelegate 的 didFinishLaunchingWithOptions
函数会观察到它。
收到通知后,我调用 User.logout
清除所有用户数据,然后应用程序在 DispatchQueue
块中崩溃。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = .white
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "LogOut"), object: nil, queue: nil) { _ in
User.logout()
DispatchQueue.main.sync {
self.navigationController?.setViewControllers([WelcomeViewController()], animated: false)
}
}
if let _ = UserDefaults.standard.string(forKey: "loggedIn") {
self.navigationController = UINavigationController(rootViewController: HomeViewController())
} else {
self.navigationController = UINavigationController(rootViewController: WelcomeViewController())
}
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
崩溃发生时没有任何附加信息。知道如何使它不崩溃吗?谢谢!
我认为你应该在调用 setViewControllers
之前 "clear" 导航堆栈,此外:
launch the main thread synchronously from a background thread before it exits
在逻辑上是错误的,最好这样使用async
:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "LogOut"), object: nil, queue: nil) { _ in
User.logout()
DispatchQueue.main.async {
self.navigationController?.popToRootViewController(animated: false)
self.navigationController?.setViewControllers([WelcomeViewController()], animated: false)
}
}
在我的 SettingsVC 中,我发送了一个 NSNotification.Name(rawValue: "LogOut")
的通知,AppDelegate 的 didFinishLaunchingWithOptions
函数会观察到它。
收到通知后,我调用 User.logout
清除所有用户数据,然后应用程序在 DispatchQueue
块中崩溃。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = .white
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "LogOut"), object: nil, queue: nil) { _ in
User.logout()
DispatchQueue.main.sync {
self.navigationController?.setViewControllers([WelcomeViewController()], animated: false)
}
}
if let _ = UserDefaults.standard.string(forKey: "loggedIn") {
self.navigationController = UINavigationController(rootViewController: HomeViewController())
} else {
self.navigationController = UINavigationController(rootViewController: WelcomeViewController())
}
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
崩溃发生时没有任何附加信息。知道如何使它不崩溃吗?谢谢!
我认为你应该在调用 setViewControllers
之前 "clear" 导航堆栈,此外:
launch the main thread synchronously from a background thread before it exits
在逻辑上是错误的,最好这样使用async
:
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "LogOut"), object: nil, queue: nil) { _ in
User.logout()
DispatchQueue.main.async {
self.navigationController?.popToRootViewController(animated: false)
self.navigationController?.setViewControllers([WelcomeViewController()], animated: false)
}
}