MB41
MB41

Reputation: 584

How do you save data when the iphone app goes to background or foreground state?

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!

Upvotes: 4

Views: 4126

Answers (2)

Duncan C
Duncan C

Reputation: 131398

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.

Upvotes: 2

iAnurag
iAnurag

Reputation: 9356

I will suggest use core data for data persistance

Please go through ray wenderlich tutorial for core data

Upvotes: 0

Related Questions