0

For the given configuration I need to be able to store should I consider using NSUserDefaults, or go to CoreData?

The configuration for the iPhone would be roughly like this, note the 3 levels so to speak:

  • Items (NSMutableArray)
    • aString: NSString
    • aString2: NSString
    • aDate: NSDate
    • aDate2: NSDate
    • aBool: BOOL
    • aTI1: NSTimeInterval
    • aTI2: NSTimeInterval
    • Keywords (NSMutableArray)
      • keyword: NSString
      • keyword: NSString

PS New to iPhone so haven't got any invested time into any particular persistance approach yet.

Greg
  • 34,042
  • 79
  • 253
  • 454

2 Answers2

2

Looking at the structure, i would recommend you to use NSDictionary to save the data at specific keys.

whether to useCore Data or NSUserDefault depends whether you want the data to be saved on a plist and be available every time you start your application or to share the data among the classes without saving it in a file, respectively.

Shrey
  • 1,959
  • 2
  • 21
  • 44
  • thanks Shrey - so just to confirm then I should be able to use NSUserDefault to store such data then - I might start looking into this approach then as I assume there would be a much less learning ramp up required... – Greg Mar 27 '11 at 10:15
  • I'm wondering with the 3 levels of items that the NSUserDefaults method might not be quite so straight forword - for example just getting a mutable copy of the structure from the immutable one provided back for example - have a question here http://stackoverflow.com/questions/5453481/how-to-do-true-deep-copy-for-nsarray-and-nsdictionary-with-have-nested-arrays-dic – Greg Mar 28 '11 at 00:32
2

NSUserDefaults and Core Data perform two different task and you should pick the one appropriate to the data you wish to persist.

Despite the historical name, the NSUserDefaults are intended as the application preferences. (In the old Next days, there was complicated set of network based preference domains of which the NSUserDefaults was merely the bottom/local most set of preference. In iOS, they have become just the preferences and the enclosing domains have been removed or hidden.) If the data you wish to persist regards the application state e.g. users choice of UI layout (such as fonts or startup mode), last open view, last viewed web page, then that information should go into the defaults.

Core Data by contrast is designed to implement the entire data model layer of a Model-View-Controller design app (which all iOS apps are.) It's primary task is not persistence/saving but modeling/simulating the logic and relationships connected to the app's data. Really, the model layer is the guts of the apps logic and the controllers and views just interface.

As such, Core Data is overkill for saving little pieces of data like you have. You can certainly do it but it's like driving a semi-truck down to the QuickyMart to haul back a quart of milk.

Perhaps more importantly, if the data is application state data then it needs to be in the user defaults so that (1) the data is available when the app starts and (2) it is available anywhere in the app.

TechZen
  • 64,370
  • 15
  • 118
  • 145