Storing data

I have always wondered how apps store data. For example, Apple's Reminders app can store a list of reminders, each containing a name, a due date, a priority, etc. I want to implement features like this is my apps. Just for an example, how would you go about creating a copy of Apple's Reminders app, just the actual reminder storage.

There are a number of ways I can think of to store just one reminder such as structures, dictionaries, etc. But I dont know how people store whole lists of items. Can anyone tell me how to do this? Thanks in advance.

Replies

I typically would either:

  • serialize the data as JSON or a PLIST or other structured file format and save into the application data directory
  • create a SQLlite database on first app startup in the data directory, and use that for persistence
  • use CoreData (not a novice technology)

The simplest is to save in NSUserDefaults


When you have changed a value:

            let defaults = NSUserDefaults.standardUserDefaults()
            defaults.setObject(valueToSave, forKey: keyForTheValue)
            defaults.synchronize()

The simplest is to save in NSUserDefaults

I recommend against that. First of all, Swift programmers should use the Swift-native

UserDefaults
rather than NSUserDefaults. Beyond that, however, the user defaults mechanism was designed for storing preferences, not the main data of your app. Before using user defaults to store some data you should ask yourself “How would the user react if this data was lost?” If the reaction is “ sigh I have to go and change a preference”, go ahead and use user defaults. If the reaction involves pitchforks and flaming torches, do the work to manage this data yourself (karim gave an excellent list of options for this).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"