Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to persist preferences of several User ( NSUserDefaults, XML file or Database)

I am creating one iOS app with the possibility of managing users and login them.

I want to persist the user data settings of every user but I´m not sure what is the best way to do save this setting by each user.

Should I save the data into an xml file, or NSUserDefaults or even saving them into my Parse Cloud Database?

I just want to save a list of user properties when loading a view, but I must have in consideration that my App must load the right parameter for current user.

For example:

User: Peter trackingSwitchEnabled: YES

User: Molly trackingSwitchEnabled: NO

User: Paul trackingSwitchEnabled: YES

like image 916
Jesús Hurtado Avatar asked Sep 26 '13 08:09

Jesús Hurtado


People also ask

How do you save NSUserDefaults in Objective C?

static func setObject(value:AnyObject ,key:String) { let pref = NSUserDefaults. standardUserDefaults() pref. setObject(value, forKey: key) pref. synchronize() } static func getObject(key:String) -> AnyObject { let pref = NSUserDefaults.

Where are NSUserDefaults stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

How much data can user defaults hold?

Currently, there is only a size limit for data stored to local user defaults on tvOS, which posts a warning notification when user defaults storage reaches 512kB in size, and terminates apps when user defaults storage reaches 1MB in size.

When should I use Core Data vs UserDefaults?

Core Data only fetches the information it needs to perform the requests the application makes. This is very different from the defaults system. The UserDefaults class loads the property list into memory to improve performance and it asynchronously writes the changes back to disk at appropriate times.


2 Answers

  • Don't use the keychain unless you are storing sensitive information.
  • Don't use Core Data or XML (unless it's a plist) because it's a lot of work.
  • Don't use Parse unless you have a reason to, because your user data would depend on you paying for an account, and the users having connectivity.
  • Use NSUserDefaults with key=user, value=JSON string. You can parse that to/from an object.
  • Use a compound key if you are extra-lazy, e.g.: key="Mary|tracking", value=YES.

Bonus ASCII art!

                           NSUserDefaults   plist    Core Data   SQList
    Full text search           ✘              ✘          ✘         ✔
    Complex search             ✘              ✘          ✔         ✔
    Binary data                ✘              ✘          ✔         ✔
    Allows complex data        ★              ★★        ★★★★     ★★★★
    Performance                ★              ★         ★★★      ★★★★
    Learning curve             ★              ★★        ★★★      ★★★★
like image 84
Jano Avatar answered Nov 15 '22 00:11

Jano


If you want to use NSUserDefaults, you should use initWithSuiteName: to create different database.

[[NSUserDefaults alloc] initWithSuiteName:USER_IDENTIFY]

like image 35
BB9z Avatar answered Nov 15 '22 00:11

BB9z