ios - iOS 13,什么时候保存数据?

标签 ios save ios13

iOS 13 中的场景支持和多窗口让何时保存数据的问题变得复杂。场景委托(delegate)的 sceneDidEnterBackground 似乎是个不错的地方,但有时它还不够:

  • 如果您的场景在最前面并且用户转到应用程序切换器并终止您的应用程序,您将得到 sceneDidDisconnectapplicationWillTerminate,而不是 sceneDidEnterBackground.

  • 如果用户在您的应用位于最前面时关闭设备,您将得到 applicationWillTerminate,而不是 sceneDidEnterBackground

人们使用什么策略来管理支持窗口场景和可能的多个窗口的 iOS 13 应用程序中的数据保存?

最佳答案

来自 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 建议我们在事件发生时保存用户数据,比如响应用户的操作(关闭 View Controller 、切换开关、在文本字段中输入文本等),但我们可能如果我们需要或想要,请使用 sceneWillResignActive(_:) 和/或 sceneDidDisconnect(_:) 来保存一些数据。

关于ios - iOS 13,什么时候保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58567287/

相关文章:

ios - 纵向模式下的尴尬横向键盘,IOS 8

javascript - Angularjs ng-disabled 无法按按钮元素的预期工作

c# - LINQ to XML - 更新节点并将其保存到 XML 文件

swift - UISearchController 中的取消按钮在 iOS 13 中没有正确消失

swift - iOS UILabel textColor 在暗模式下不更新

ios - 以核心文本为中心绘制 NSAttributedString

ios - 在 Collection View Swift 中处理 n 个 gif

ios - 在 UITableView 上激活 Speech to Text

ios - 无法保存 plist ios

ios - SuperpoweredAdvancedAudioPlayer 无法在 iOS13 中打开 "ipod-library"路径