gpt4 book ai didi

ios - 注销用户会使应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:54 24 4
gpt4 key购买 nike

在我的 SettingsVC 中,我发送了一个 NSNotification.Name(rawValue: "LogOut") 的通知,AppDelegate 的 didFinishLaunchingWithOptions 函数会观察到它。

收到通知后,我调用 User.logout 清除所有用户数据,然后应用程序在 DispatchQueue block 中崩溃。

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 之前“清除”导航堆栈,此外:

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)
}
}

关于ios - 注销用户会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48026115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com