iOS状态恢复、用户默认值和SQLite之间的区别是什么?

huangapple go评论82阅读模式
英文:

Difference between iOS state restoration VS User Defaults and SQLite?

问题

这些说明表明状态恢复方法的主要目的是在应用程序的生命周期“跨越”时保留其状态,特别是当应用程序已经被终止并完全从内存中移除。这是因为如果应用程序仅暂时挂起然后稍后恢复,当我们回来时,任何当前存在于内存中的变量和对象仍将存在。

如果这是真的,那么application(_:shouldRestoreSecureApplicationState:)的目的是什么?当我们已经拥有诸如用户默认设置(UserDefaults)和SQLite等工具时,我们真的需要它吗?或者它只是现有工具之上的内置层?

(Note: The code segment is excluded from translation as requested.)

英文:

What I currently know:

  • When an app is suspended, its memory space is not deallocated, but the app's code execution is paused. This means that any variables and objects that are currently in memory will still be there when the app resumes.

  • However, if the app remains suspended for an extended period of time or if the system needs to free up memory for other apps, the operating system may choose to terminate the app to free up resources. When an app is terminated, all of its memory and data are released, and the next time the app is launched, it will start from scratch.

  • If your app has implemented state preservation and restoration, any data that you've saved using the encodeRestorableState method will be available when the app resumes, even if the app was terminated in the meantime.

  • If your app hasn't implemented state preservation and restoration, any data that wasn't saved before the app was suspended or terminated will be lost. It would be like starting from scratch on the home screen.

What I am confused about:

These statements imply that the main purpose of the state restoration methods is to preserve the state of an app "across" its lifecycle, specifically when an app has been terminated and removed from memory entirely. This is because if the app is only temporarily suspended and later resumed, any variables and objects currently in memory will still exist when we come back.

If that's true, what's the purpose of application(_:shouldRestoreSecureApplicationState:)? Do we really need it when we already have tools such as User Defaults and SQLite? Or is it simply a built-in layer on top of these existing tools?

答案1

得分: 1

应用程序状态与应用程序数据不同。

以文字处理器为例:

用户正在操作的文档内容是应用程序的数据
他们正在操作的文档名称和所选的字体样式是应用程序的状态的示例。

在iOS 4之前,没有应用程序挂起状态;当用户按下主屏幕按钮时,活动应用程序会被终止,并在他们点击应用程序图标时重新启动。

应用程序状态的保留对于给予用户应用程序实际在运行而不仅仅是在用户接听电话后将用户带回应用程序的初始屏幕的印象很重要。

尽管今天应用程序实际被终止之前可能需要一些时间,但如果您的应用程序具有用户期望保持的复杂状态,那么采用状态恢复非常重要。例如,对于文字处理器,状态恢复可能至关重要。对于始终显示最新“动态消息”内容的社交媒体应用程序,状态恢复可能根本不重要。

虽然您可以使用UserDefaults甚至Core Data来存储和检索应用程序状态,但encodeRestorableState为每个UIViewController子类提供了一种优雅的方式,以隔离方式提供其状态给恢复过程,而不会紧密耦合UIViewController类与某个状态恢复对象,如果您使用Core Data或UserDefaults,这种情况可能会发生。

英文:

The application state is different to the application data.

To use a word processor as an example:

The content of the document the user is working on is application data.
The name of the document they are working on, and the font style they had selected are examples of application state.

Prior to iOS 4, there was no app suspension state; When the user pressed the home button, the active app was terminated and relaunched when they tapped the application icon.

App state preservation was important to give the impression that the app had actually kept running and not just drop the user back at the initial screen of the app after, say, they answered a phone call.

Although today it can take some time before an application is actually terminated, if your application has complex state that the user would expect to be maintained, then it is important to adopt state restoration. E.g. For a word processor, state restoration might be vital. For a social media app that always displays the most recent "feed", state restoration may not be important at all.

While you can use UserDefaults or even Core Data to store and retrieve application state, encodeRestorableState provides an elegant way for each UIViewController subclass to provide its state to the restoration process in isolation and without tightly coupling UIViewController classes to some state restoration object, which would happen if you used Core Data or UserDefaults.

huangapple
  • 本文由 发表于 2023年2月18日 23:36:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定