状态恢复仅在连接到 Xcode 时才有效 [英] State restoration only works when connected to Xcode

查看:28
本文介绍了状态恢复仅在连接到 Xcode 时才有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行计时器的应用程序,即使应用程序退出或手机关机,计时器也应该继续运行.所以我尝试使用 shouldSaveApplicationStateshouldRestoreApplicationState 来做到这一点.我将这两个方法和 willFinishLaunchingWithOptions 添加到我的 appDelegate 中,并为每个涉及的视图控制器、导航控制器和标签栏控制器设置了恢复 ID.然后在我想恢复的视图控制器上我做了这个:

I have an app that runs a timer, and the timer should keep running even if the app quits or the phone is turned off. So I'm trying to do that using shouldSaveApplicationState and shouldRestoreApplicationState. I added both methods and willFinishLaunchingWithOptions to my appDelegate and I set up restoration IDs for every view controller, navigation controller and tab bar controller involved. Then on the view controller I want to restore I did this:

override func encodeRestorableStateWithCoder(coder: NSCoder) {
    coder.encodeObject(startDate, forKey: "startDate")
    coder.encodeObject(startTime, forKey: "startTime")
    coder.encodeObject(elapsedTime, forKey: "elapsedTime")
    coder.encodeObject(playing, forKey: "playing")
    coder.encodeObject(timerLabel.text, forKey: "timerLabelText")
    super.encodeRestorableStateWithCoder(coder)
}

override func decodeRestorableStateWithCoder(coder: NSCoder) {
    startDate = coder.decodeObjectForKey("startDate") as! NSDate
    startTime = coder.decodeObjectForKey("startTime") as! NSTimeInterval
    elapsedTime = coder.decodeObjectForKey("elapsedTime") as! NSTimeInterval
    playing = coder.decodeObjectForKey("playing") as! Bool
    timerLabel.text = (coder.decodeObjectForKey("timerLabelText") as! String)
    super.decodeRestorableStateWithCoder(coder)
}

override func applicationFinishedRestoringState() {
    if playing {
        elapsedTime += startDate.timeIntervalSinceNow
        play()
    }
}

现在是奇怪的部分.当我的手机连接到 Xcode 并且我使用 Xcode 的播放和停止按钮启动和退出应用程序时,一切正常.然而,当我在手机与 Xcode 断开连接的情况下尝试同样的事情时,就像我根本没有设置状态恢复一样,应用程序完全忽略它,只显示第一个视图控制器.而且我什至无法调试,因为当我将手机连接到 Xcode 时它就可以了.同样的事情发生在模拟器上.如果我使用 Xcode 的按钮,则恢复工作.如果我只是从模拟器本身打开和关闭应用程序,则不会.

Now here's the weird part. When my phone is connected to Xcode and I use Xcode's play and stop buttons to start and to quit the app everything works as it should. When I try the same thing with the phone disconnected from Xcode, though, it's like I didn't even set up the state restoration at all, the app ignores it completely and just shows the first view controller. And I can't even debug because when I connect the phone to Xcode it works out. The same thing happens on the simulator. If I use Xcode's buttons the restoration works. If I just open and close the app from the simulator itself, it doesn't.

有什么想法吗?

推荐答案

当用户从多任务菜单主动杀死"您的应用程序时,状态恢复不起作用.它仅在系统在后台默默杀死您的应用以回收资源(例如内存)时才有效.

State restoration does not work when the user actively "kills" your app from the multitask menu. It only works when the system silently kills your app in the background to reclaim resources (e.g., memory).

基本原理(以下是我自己的推测/解释)是,状态恢复的全部目的是让用户回到上次离开的应用程序,因为如果它从未被终止(从用户的角度来看).

The rationale (what follows is my own speculation/interpretation) would be that, the whole purpose of state restoration is for the user to come back to the app as they left it last time, as if it never was terminated (from the user's point of view).

但如果用户明确终止应用,则意味着他们不期望它仍在运行,就像他们离开时一样".

But if the user explicitly kills the app, it means they don't expect it to be "still running, as they left it".

来源:Apple 文档的这一部分 指出:

  • 当用户强制退出应用时,系统会自动删除应用的保留状态. 删除保留状态信息当应用程序被杀死时是一种安全预防措施.(作为安全预防措施,如果应用程序崩溃,系统也会删除保留的状态在启动期间两次.)如果你想测试你的应用程序的能力恢复它的状态,你不应该使用多任务栏来杀死调试期间的应用程序.相反,使用 Xcode 来终止应用程序或终止应用程序通过安装临时命令或手势以编程方式应用程序按需调用退出.
  • The system automatically deletes an app’s preserved state when the user force quits the app. Deleting the preserved state information when the app is killed is a safety precaution. (As a safety precaution, the system also deletes preserved state if the app crashes twice during launch.) If you want to test your app’s ability to restore its state, you should not use the multitasking bar to kill the app during debugging. Instead, use Xcode to kill the app or kill the app programmatically by installing a temporary command or gesture to call exit on demand.

从 Xcode 中杀死应用程序(停止按钮")复制了非用户启动的终止",因此它尊重状态保存/恢复流程.

Killing the app from Xcode ("stop button") replicates the "non-user initiated termination", hence it respects state preservation/restoration flow.

这篇关于状态恢复仅在连接到 Xcode 时才有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆