iOS 13、什么时候保存数据?
In iOS 13, when to save data?
场景支持和 iOS13 中的多个 windows 使何时保存数据的问题变得复杂。场景代表的 sceneDidEnterBackground
似乎是个不错的地方,但有时它还不够:
如果您的场景在最前面并且用户转到应用程序切换器并终止您的应用程序,您将得到 sceneDidDisconnect
和 applicationWillTerminate
,而不是 sceneDidEnterBackground
.
如果用户在您的应用位于最前面时关闭设备,您将得到 applicationWillTerminate
,而不是 sceneDidEnterBackground
。
在 iOS 13 个支持 window 场景并且可能支持多个 windows 的应用程序中,人们使用什么策略来管理数据保存?
来自 UISceneDelegate.sceneWillResignActive(_:)
的文档:
If your scene has unsaved user data, save that data here to ensure that it isn't lost. However, never save data solely from this method. Instead, save it at appropriate points from your view controllers, usually in response to user actions. For example, save data when the user dismisses a data-entry screen. Do not rely on specific app transitions to save all of your app's critical data.
他们还在 UISceneDelegate.sceneDidDisconnect(_:)
的文档中说明:
Use this method to perform any final cleanup before your scene is purged from memory. For example, use it to release references to files or shared resources and to save user data.
所以看起来 Apple 建议我们在事件发生时保存用户数据,比如响应用户的操作(关闭视图控制器、切换开关、在文本字段中输入文本等),但是如果需要或想要,我们可以使用 sceneWillResignActive(_:)
and/or sceneDidDisconnect(_:)
来保存一些数据。
场景支持和 iOS13 中的多个 windows 使何时保存数据的问题变得复杂。场景代表的 sceneDidEnterBackground
似乎是个不错的地方,但有时它还不够:
如果您的场景在最前面并且用户转到应用程序切换器并终止您的应用程序,您将得到
sceneDidDisconnect
和applicationWillTerminate
,而不是sceneDidEnterBackground
.如果用户在您的应用位于最前面时关闭设备,您将得到
applicationWillTerminate
,而不是sceneDidEnterBackground
。
在 iOS 13 个支持 window 场景并且可能支持多个 windows 的应用程序中,人们使用什么策略来管理数据保存?
来自 UISceneDelegate.sceneWillResignActive(_:)
的文档:
If your scene has unsaved user data, save that data here to ensure that it isn't lost. However, never save data solely from this method. Instead, save it at appropriate points from your view controllers, usually in response to user actions. For example, save data when the user dismisses a data-entry screen. Do not rely on specific app transitions to save all of your app's critical data.
他们还在 UISceneDelegate.sceneDidDisconnect(_:)
的文档中说明:
Use this method to perform any final cleanup before your scene is purged from memory. For example, use it to release references to files or shared resources and to save user data.
所以看起来 Apple 建议我们在事件发生时保存用户数据,比如响应用户的操作(关闭视图控制器、切换开关、在文本字段中输入文本等),但是如果需要或想要,我们可以使用 sceneWillResignActive(_:)
and/or sceneDidDisconnect(_:)
来保存一些数据。