【发布时间】:2020-09-25 19:22:56
【问题描述】:
我正在尝试在我的应用程序中实现状态恢复,我已经阅读了许多文章和文档,但到目前为止我还不能让它工作。我已经为所有视图控制器提供了一个恢复 ID,并且(我认为)具有使其工作所需的所有必要功能。
在AppDelegate
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreSecureApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldSaveSecureApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, viewControllerWithRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
return coder.decodeObject(forKey: "Restoration ID") as? UIViewController
}
func application(_ application: UIApplication, didDecodeRestorableStateWith coder: NSCoder) {
UIApplication.shared.extendStateRestoration()
DispatchQueue.main.async {
UIApplication.shared.completeStateRestoration()
}
}
在我的视图控制器中:
override func encodeRestorableState(with coder: NSCoder) {
super.encodeRestorableState(with: coder)
getSetDataFromTable()
coder.encode(currentSession, forKey: "CurrentSession")
}
override func decodeRestorableState(with coder: NSCoder) {
super.decodeRestorableState(with: coder)
self.currentSession = coder.decodeObject(forKey: "CurrentSession") as! [CurrentSessionElement]
}
override func applicationFinishedRestoringState() {
tableView.reloadData()
}
static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
guard let restoredSession = coder.decodeObject(forKey: "CurrentSession") as? [CurrentSessionElement] else {
print("decoding user detail")
return nil
}
let vc = CurrentSessionVC()
vc.currentSession = restoredSession
return vc
}
我在所有函数中都设置了断点,当我尝试测试功能时遇到的断点是
加载时:
shouldRestoreSecureApplicationState
didDecodeRestorableStateWith
清除应用程序时:
shouldSaveSecureApplicationState
测试时没有恢复,也没有触发任何视图控制器功能,我错过了什么吗?
【问题讨论】:
标签: ios swift state-restoration