Is there a restriction on storing values ​​in NSUserDefaults?

I am new to iPhone design and objective C I used NSUserDefaults to store some values ​​in my application. But I do not know if there is any limit for storing values ​​in NSUserDefaults . Can anyone help me find out this.

Thanks in advance.

+61
ios objective-c iphone xcode nsuserdefaults
Sep 22 '11 at 5:53
source share
9 answers

As long as there is enough space on the iPhone / iPad, you can store NSUserDefault values. All these values ​​are stored in a .plist file, and this file is very small, most of the time it is less than 1 kb (if you do not store a lot of data).

+48
Sep 22 '11 at 5:59
source share

There are restrictions on the types that you can store: all of them must be objects of a property list, namely NSString , NSNumber , NSData , NSArray and NSDictionary . In addition, you can store NSArray and NSDictionary if the values ​​are also objects in the property list; also all NSDictionary keys must be strings.

Note that an object like UIColor is not listed in the list above. Therefore, if you want to save the color in the default database, you first need to convert it to a string or data object, and then convert back when reading the default values.

As for the size restrictions, they are not documented, but note that all data will be saved as a property list file. The entire file is read and written as a whole, so if you use NSUserDefaults to store large amounts of data that only change in parts, you will spend a lot of time on unnecessary I / O operations.

+42
Sep 22 2018-11-11T00:
source share

Each answered a direct question: "Is there a limit?" However, I found this chain that really wants to understand " what is too much to store in UserDefaults?"

If you are looking for this answer, thread is useful here. The answers that I found useful were to go to the project file and see the size of the plist file:

5 objects almost nothing. You will be fine!




On my machine, I have about 28 megabytes of data in my default settings. This does not cause any problems.




From my general programming experience with arrays, I would suggest that performance starts to decay quickly when you fall in 1000, depending on the size of the element. Therefore, in the program I would not have a problem with storing several hundred elements. This says that I will probably start using the sqlite3 or coredata database sooner rather than later if you were you.

Important to remember:

The above mitigated my concern that a growing number of defaults (around 20-25 now) would cause problems. I already use CoreData, so I considered what to use, since the number of allowed user settings / settings is increasing. So, I'm going to stay with user defaults.

However , as other answers pointed out, the file will be read and written as a whole. So reading 20 key / string dictionaries and 5 key / boolean dictionaries just to extract a single line ... is not entirely ideal. However, if that doesn't hurt performance, and it saves you a ton of code, why not?

+8
Jan 12 '17 at 2:19 on
source share

There are no restrictions for storing values ​​in NSUserDefaults.

+4
Sep 22 '11 at 6:00
source share

The only NSUserDefaults Storage Limit is Storage Capacity .

How accessible Storage space is on an iOS device , you can store data in NSUserDefaults . Couple - the value is stored in a structured xml file (.plist), which is stored in the App Bundle .

The default system and default keystore are for storing simple data types - strings, numbers, dates, boolean values, URLs, data objects, etc. - in the property list . Using a property list also means that you can organize your preference data using an array and types of dictionaries. It is also possible to store other objects in the property list by first copying them to an NSData object.

+4
Jan 21 '16 at 7:06
source share

As many have already mentioned: I do not know of any size limits (other than physical memory) for storing data in a .plist (for example, UserDefaults). So it’s not a question of how much.

The real question should be HOW often do you write new / changed values ​​... And this is due to the discharge of the battery that will cause this entry.

IOS has no chance to avoid physically writing to the "disk" when changing a single value, just to maintain data integrity. For UserDefaults, this causes the entire file to be overwritten to disk.

This includes the “drive” and supports it for a longer time and prevents the IOS from switching to low power mode.

From the "Energy Efficiency Guide for iOS Applications":

Minimize data recording. Write to files only when their contents have changed, and merge the changes into one record when possible. Avoid writing the entire file if only a few bytes have changed. If you often change small parts of large files, consider using a database to store data.

READINGS are not a problem, since all values ​​are cached in memory.

EDIT: (July 2019): I just found this very nice Jeffrey Fulton blog post.

https://jeffreyfulton.ca/blog/2018/02/userdefaults-limitations-and-alternatives

He describes in detail various aspects of user default settings, and writes about some performance tests.

+4
Dec 22 '17 at 11:14
source share

From iOS SDK codes and the corresponding official Apple document. ,

 extension UserDefaults { /*! NSUserDefaultsSizeLimitExceededNotification is posted on the main queue when more data is stored in user defaults than is allowed. Currently there is no limit for local user defaults except on tvOS, where a warning notification will be posted at 512kB, and the process terminated at 1MB. For ubiquitous defaults, the limit depends on the logged in iCloud user. */ @available(iOS 9.3, *) public class let sizeLimitExceededNotification: NSNotification.Name // .... } 




Summary

  1. There are currently no default restrictions for local users.
  2. On tvOS , where a warning notification will be posted at 512 KB and the process completed at 1 MB.
  3. For ubiquitous defaults, the restriction depends on the iCloud user logged in.
+4
Jun 12 '18 at 7:29
source share

As far as I know, there are no storage limits in NSUserdefaults.

+2
Sep 22 2018-11-11T00:
source share

This is independent of the maximum allowed file size on disk. You can use this piece of code to test it!

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *myKey = @"key"; int sizeOfFile = 1024; // Fill in proper file size here NSData myObject; NSString *someFilePath = @"PathToYourFileHere"; for(int i = 1; i < 9999999999999999; i++) { myObject = [NSData dataWithContentsOfFile:someFilePath]; [defaults setObject:myObject forKey:[NSString stringWithFormat:@"%@%i", myKey, i]]; NSLog(@"Iteration: %i, TotalWritten: %i", i, i * sizeOfFile); } 
0
Jan 04 '15 at 15:40
source share



All Articles