当iphone应用程序进入后台或前台状态时,如何保存数据?(How do you save data when the iphone app goes to background or foregro

编程入门 行业动态 更新时间:2024-10-23 08:30:09
当iphone应用程序进入后台或前台状态时,如何保存数据?(How do you save data when the iphone app goes to background or foreground state?)

我有一个应用程序,用户输入某些信息。 每次用户想要时,他都会在表格列表中添加新项目。 用户完成后,他们单击完成按钮以完成列表,他们可以启动另一个列表。

在AppDelegate.swift文件中,我看到了一些预先存在的函数。 applicationDidEnterBackground表示在应用程序进入后台之前保存信息(我假设用户在使用应用程序时按下主页按钮...)。

在我的应用程序中,每次用户将项目添加到列表中,然后进入主屏幕并锁定手机,一段时间后项目消失。 所以他们添加的信息不能保存。 我想在每次应用程序最小化或最大化时使用AppDelegate.swift的函数来保存信息和加载信息。 我的问题是,保存这些信息的最佳方法是什么? 我只是写一个文件并从文件中读取,还是有办法自动保存和加载这些信息? 我已经阅读了有关将对象保存到.plist但我不一定知道它是如何工作的,因为当我单击info.plist它只需要我设置表而没有代码。

提前谢谢你的帮助!

I have an app where the users enters certain information. Each time the user wants to, he adds a new item to a table list. When the user is done, they click the finalize button to finish the list and they can start another list.

In the AppDelegate.swift file, I see a few preexisting functions. applicationDidEnterBackground says to save information before the application enters the background (I'm assuming it's when the user presses the home button while using the app...).

In my app, each time the user adds items to the list, then goes to home screen and locks the phone, after a while the items are gone. So the information they added don't save. I'm thinking to use the functions in AppDelegate.swift to save information and load information each time the app is minimized or maximized. My question is, what is the best way to save this information? Would I just write to a file and read from a file or is there a way to automatically save and load this information? I've read something about saving objects to a .plist but I don't necessarily know how it works since when I click the info.plist it just takes me to table of settings and no code.

Thank you in advance for the help!

最满意答案

免责声明:我是一位经验丰富的Objective-C开发人员,但对Swift来说却是一个新手。 我的答案将是面向目标的。

你有很多选择。 另一张海报建议使用Core Data,但除非你有复杂的信息需要保存,否则这可能有点过分。 核心数据是一个庞大而复杂的框架,具有陡峭的学习曲线。 它非常强大,但是在你能够流利地使用它之前,我希望能花一到两周的紧张学习。

最简单,最干净的方法通常是使用NSUserDefaults 。 它是一个Cocoa类,可以读取/写入应用程序沙箱中系统维护文件的键/值对。 (基本上是一个自动读/写到系统文件的字典。)你不需要知道它的工作原理的细节 - 它只是起作用。 有关更多信息,请参阅NSUserDefaults上的Xcode文档。

另一种选择确实是使用属性列表。 像NSArray和NSDictionary这样的Cocoa容器对象包括在属性列表中保存和读取自身的方法。 寻找像writeToFile:atomically:这样的方法writeToFile:atomically: 。

要保存到NSUserDefaults和属性列表,您保存的对象及其包含的任何对象(如果它是容器对象)必须是“属性列表”对象(字典,数组,字符串,数字(整数和浮点数),日期,二进制数据和布尔值。)在“属性列表类型和对象”的Xcode文档中搜索有关属性列表和支持的数据类型的更多信息。

第三种选择是在要保存的所有对象(整个“对象图”)中实现NSCoding协议,然后使用NSKeyedArchiver方法archiveRootObject:toFile:将根对象(通常是容器对象)保存到磁盘archiveRootObject:toFile:

使用所有这些选项,您可以实现app delegate方法applicationDidEnterBackground ,并在调用时保存状态数据。 然后,您将在applicationDidFinishLaunching方法中重新加载数据。 (当您返回前台时,无需加载已保存的状态数据,因为如果您返回前台,您的应用程序仍在运行,并且状态数据仍在内存中。)

各种Cocoa触摸类还包括对自动状态恢复的支持,但这比我有时间在这里介绍。

Disclaimer: I am an experienced Objective-C developer, but quite new with Swift. My answers are going to be Objective-C oriented.

You have lots of options. The other poster suggested Core Data, but that is likely overkill unless you have complex information to save. Core Data is a big, complex framework with a steep learning curve. It's very powerful, but expect to spend a week or two of intense study before you're able to use it with any fluency.

The easiest and cleanest way is often to use NSUserDefaults. It's a Cocoa class that reads/writes key/value pairs to a system-maintained file in your app's sandbox. (Essentially a dictionary that is automatically read/written to a system file.) You don't need to know about the details of how it works - it just works. See the Xcode docs on NSUserDefaults for more information.

Another option is indeed to use a property list. Cocoa container objects like NSArray and NSDictionary include methods to save and read themselves to/from property lists. Look for methods like writeToFile:atomically:.

For saving to both NSUserDefaults and property lists, the object you save, and any object(s) it contains (if it's a container object) must be members of a very short list of "property list" objects (dictionaries, arrays, strings, numbers (integer and float), dates, binary data, and Boolean values.) Do a search in the Xcode docs on "Property List Types and Objects" for more information on property lists and the supported data types.

A third option is to implement the NSCoding protocol in all the objects that you want to save (your entire "object graph") and then to save the root object (often a container object) to disk using the NSKeyedArchiver method archiveRootObject:toFile:

With all of these options, you can implement the app delegate method applicationDidEnterBackground, and save your state data when it's called. You'd then load the data back in in your applicationDidFinishLaunching method. (You don't need to load your saved state data back when you return to the foreground, because if you are returning to the foreground, your app is still running and it's state data is still in memory.)

Various of the Cocoa touch classes also include support for automatic state restoration, but that's more than I have time to cover here.

更多推荐

本文发布于:2023-07-23 00:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:前台   应用程序   后台   状态   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!