在iOS 13中,何时保存数据? [英] In iOS 13, when to save data?
问题描述
iOS 13中的场景支持和多个窗口使何时保存数据的问题变得复杂.场景代表的 sceneDidEnterBackground
似乎很不错,但是有时还不够:
Scene support and multiple windows in iOS 13 have complicated the question of when to save data. A scene delegate's sceneDidEnterBackground
might seem like a pretty good place, but there are times when it won't be sufficient:
-
如果您的场景位于最前面,并且用户转到应用切换器并终止了您的应用,则将获得
sceneDidDisconnect
和applicationWillTerminate
,而不是sceneDidEnterBackground
.
如果用户在您的应用程序位于最前面时关闭设备,则会得到 applicationWillTerminate
,而不是 sceneDidEnterBackground
.
If the user switches off the device while your app is frontmost, you'll get applicationWillTerminate
, not sceneDidEnterBackground
.
人们使用什么策略来管理支持窗口场景以及可能多个窗口的iOS 13应用程序中的数据保存?
What strategy are people using to manage data saving in iOS 13 apps that support window scenes and possibly multiple windows?
推荐答案
From the docs for UISceneDelegate.sceneWillResignActive(_:)
:
如果场景中有未保存的用户数据,请将其保存在此处以确保不会丢失.但是,切勿仅通过此方法保存数据.取而代之的是,通常在响应用户操作时,将其保存在视图控制器的适当位置.例如,当用户关闭数据输入屏幕时,保存数据.不要依赖特定的应用程序转换来保存应用程序的所有关键数据.
他们还在文档中声明了 UISceneDelegate.sceneDidDisconnect(_:)
:
They also state in the docs for UISceneDelegate.sceneDidDisconnect(_:)
:
使用此方法在从内存中清除场景之前执行所有最终清理.例如,使用它可以释放对文件或共享资源的引用,并使用保存用户数据.
因此,Apple似乎建议我们在事件发生时保存用户数据,例如响应用户做某事(关闭视图控制器,切换开关,在文本字段中输入文本等),但是我们可能会如果需要,请使用 sceneWillResignActive(_:)
和/或 sceneDidDisconnect(_:)
保存一些数据.
So it looks like Apple recommends that we save user data as events happen, like in response to the user doing something (dismissing a view controller, toggling a switch, entering text into a text field, etc.), but we may use sceneWillResignActive(_:)
and/or sceneDidDisconnect(_:)
to save some data if we need or want to.
这篇关于在iOS 13中,何时保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!