Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSUserdefault and core data

what is NsUserdefault ? and how it can be used to store peristent data of app . Please clear my doubt on when to use them and how they can be useful performance wise ?

like image 439
Akshay Mehta Avatar asked May 20 '26 06:05

Akshay Mehta


2 Answers

With the NSUserDefaults class, you can save settings and properties related to application or user data.
For example, you could save a profile image set by the user or a default color scheme for the application. The objects will be saved in what is known as the iOS “defaults system”. The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions. This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app.

Performance

The NSUserDefaults class caches the values internally so the lookup is extremely fast. The overhead of [NSUserDefaults standardUserDefaults] vs an instance variable is so small that you wouldn't even notice it if you did it 5 million times in your code.

like image 125
pankaj raghav Avatar answered May 21 '26 20:05

pankaj raghav


The NSUserDefaults class acts very much like something called a Property List (aka plist). Plists are limited in what kind of objects they can store. The six types plists can store are:

  1. NSData
  2. NSString
  3. NSNumber
  4. NSDate
  5. NSArray
  6. NSDictionary

Usage: NSUserDefaults is used to store values that belongs to users settings and needs to be remembered even when app is killed and re-launched. Having said that, as posted in another answer, we should put only light weighted objects in NSUserDefaults.

For other heavy wight save and reload user file system. For things that involve search on saved data use Core Data.

Writing To NSUserDefaults:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("Your Object", forKey: "yourKey")

Reading from NSUserDefaults:

let defaults = NSUserDefaults.standardUserDefaults()

if let value = defaults.stringForKey("yourKey")
{
    print(value)
}

Other Useful Convenience Methods:

func setBool(value: Bool, forKey defaultName: String)
func setInteger(value: Int, forKey defaultName: String)
func setFloat(value: Float, forKey defaultName: String)
func setDouble(value: Double, forKey defaultName: String)
func setObject(value: AnyObject?, forKey defaultName: String)
func setURL(url: NSURL, forKey defaultName: String)

Storage Size:

As long as there's enough space on the iPhone/iPad, you can store NSUserDefault values. All those values is stored into a .plist file, and this file is very small, most of the time under 1 kb. But keep this as light as possible.

like image 30
Abhinav Avatar answered May 21 '26 20:05

Abhinav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!