0

i am in love with core data in the few last months, and used it to save data in my app including the user settings. it works great but i am afraid that i will have problem with future updates.

i know 3 ways to store preferences in my app - 1. core data. 2. NSUserDefaults. 3. using the build in setting component.

love to understand the pros and cons of this approaches.

thanks shani

shannoga
  • 19,649
  • 20
  • 104
  • 169
  • hmmm a bit off topic, but can I know where you learn core data from? I've always avoided core data because I always thought plist and preferences are enough... (and I know advantages or core data is because it can do sql queries) – Enrico Susatyo Jan 14 '11 at 10:34
  • hi. i had an app build in with plists and i have to say that moving to core data made my life a lot easier and opened many more options for my app. i personally learned by reading apple examples projects and by experimenting and building simple apps with core data. it took time and i still have a lot to learn but i truly believe that the best way is to try and try and try until one day you simply get it. good luck. – shannoga Jan 14 '11 at 12:47

1 Answers1

4

Edit note: My previous link, Implementing Application Preferences, is broken. The rest of this reply should still apply. However, you can now also store data in iCloud in a manner that's similar to (but separate from) NSUserDefaults.

You can refer to Apple's own guide: About Preferences and Settings

First, you can store settings however you want: It's just user data. The choice between NSUserDefaults and Core Data is just between API, where the former is designed specifically to handle user preferences. But you could (technically) use INI files, if you absolutely wanted to (but don't!). So there are more than 3 ways to do it.

Second, NSUserDefaults and the "built-in settings component" are really one and the same. Using the settings app will still store preferences in NSUserDefaults which you access in your app with that API.

The reason why you might not want to use the built-in settings app would be: It's cumbersome for users to change those settings. If you have settings that users might want to change frequently, you might want to do that inside your app (e.g. turning music on/off, changing player name). Also, since you have full control over your own app, you can have a more flexible GUI than the one Apple provides in Settings.app

But it's really a question of GUI, not how settings are stored.

As for using Core Data or NSUserDefaults... well, as I said, one is designed specifically for user preferences, the other for row-based user data. NSUserDefaults lets you have default settings, is trivial to add new settings to and supports the built-in settings system. It's also quite simple: It just loads a property list. Core Data on the other hand requires the whole persistent store and managed object context stack.

To summarize: Use NSUserDefaults for settings, and choose between the built-in settings app and having your own control panel based on how often settings must be changed and your specific needs.

vicvicvic
  • 6,025
  • 4
  • 38
  • 55
  • Hi vicvicvic. thanks first of all. will you still prefer using NSUserDefaults even if the app is already uses core data, which mean i only have to set an other entity for user preferences? – shannoga Jan 14 '11 at 12:43
  • YES I would! NSUserDefaults is a really lightweight system which hooks neatly into how settings work. With NSUserDefaults, getting a setting is as easy as [[NSUserDefaults sharedUserDefaults] stringForKey:@"kSettingsKey"]; -- Doing the same in Core Data requires mucking around with an object context, fetch requests and persistent stores... – vicvicvic Jan 14 '11 at 12:55
  • Hi vicvicvic, Do you know it there is a way to use the NSUserdefault api but not with the standardUserDefaults object. I'd like the preferences to be stored in my application so when the application is deleted, it doesn't keep the settings in the system. The reason is that I want to save a value for each page of a book and I don't want to put thousands of settings in the global system preferences. – CedricSoubrie Mar 29 '11 at 14:55
  • Sorry about the late (and insecure) reply. I'm not sure if there is, but there should be... I'm pretty sure you can define some kind of custom defaults "domain" as they call it ... or maybe they left that half-implemented. There might be a difference between OS X and iOS here; I believe the API on iOS is more basic. – vicvicvic Apr 10 '11 at 01:31