You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

NSUserDefaults storage size capacity?

Does anyone know the capacity of stuff I can pack into NSUserDefaults? I've checked the docs and it doesn't give a size, ie. 5mb, etc.

I'm planning on storing some thumbnail images (their NSData) and then on restart of app recreate those images from the NSData.

Thanks

macbook, Mac OS X (10.5.4)

Posted on Oct 22, 2008 3:58 PM

Reply
11 replies

Oct 23, 2008 1:53 PM in response to orangekay

If you aren't going to add any helpful or relevant information, I ask that you please stop posting replies to any of my threads. Thanks.

Anyone else? I'm hoping that perhaps it's mentioned somewhere in the docs where I haven't seen it, or that someone has already experienced some limitations when saving stuff to the NSUserDefaults.

Oct 23, 2008 2:39 PM in response to applehund

It really is the max file size. All it takes is to write a simple little loop to insert data until you run out of space like orange said. I'll even write it for you:


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

Jan 23, 2009 9:27 AM in response to applehund

I have a slightly different take on this question: what's the maximum size of an object that you can put into the user defaults. I'm trying to put an NSString with a length of 56 into the defaults (using "sessionID" as my key) and this is causing the application to crash in the SDK2.2 simulator at a later time when it attempts to sync the user defaults out into the file system (at which point I also see a 0-length file with a garbage name in the Library/Preferences directory next to my normal application preference file). It's almost as if someone put in a fixed buffer size somewhere and my relatively short string overran the buffer. The stack trace I see is:

[code]
#0 0x96817688 in objc_msgSend ()
#1 0x94acc256 in CFGetTypeID ()
#2 0x94abd389 in __CFPropertyListIsValidAux ()
#3 0x94abdc59 in __CFPropertyListIsDictPlistAux ()
#4 0x94a9ab29 in CFDictionaryApplyFunction ()
#5 0x94abd455 in __CFPropertyListIsValidAux ()
#6 0x94abd531 in CFPropertyListIsValid ()
#7 0x94abf749 in CFPropertyListWriteToStream ()
#8 0x94b34952 in +[CFXPreferencesPropertyListSource writePlist:toURL:withMode:owner:group:format:statInfo:] ()
#9 0x94b34a80 in -[CFXPreferencesPropertyListSource synchronize] ()
#10 0x94b311c4 in -[CFXPreferencesSearchListSource synchronize] ()
#11 0x94b36747 in CFPreferencesAppSynchronize ()
#12 0x90104985 in -[NSUserDefaults(NSUserDefaults) synchronize] ()
#13 0x900f2e23 in __NSFireTimer ()
#14 0x94acab25 in CFRunLoopRunSpecific ()
#15 0x94acacd8 in CFRunLoopRunInMode ()
#16 0x31564600 in GSEventRunModal ()
#17 0x315646c5 in GSEventRun ()
#18 0x30a4ec98 in -[UIApplication _run] ()
#19 0x30a5a094 in UIApplicationMain ()

The strange thing is it only seems to happen when I also have code to retrieve the default ...

Casey

Jan 25, 2009 11:37 AM in response to applehund

While the theoretical file size limit for property lists may indeed be 2 GB, that doesn't mean that they're necessarily the best approach for storing large amounts of binary data.

See Property List Programming Guide: When to Use Property Lists

Property List Programming Guide: When to Use Property Lists

Many applications require a mechanism for storing information that will be needed at a later time. For situations where you need to store small amounts of persistent data—say less than a few hundred kilobytes—property lists offer a uniform and convenient means of organizing, storing, and accessing the data.

In some situations, the property list architecture may prove insufficient. If you need a way to store large, complex graphs of objects, objects not supported by the property list architecture, or objects whose mutability settings must be retained, use archiving.....

...Note that property lists should be used for data that consists primarily of strings and numbers. They are very inefficient when used with large blocks of binary data.



It sounds like what you'd like to do should be okay, but it would definitely depend on how large the thumbnail images are, and how many you plan on storing. What I would probably do is use the Property List Editor developer application to take a look at any Apple applications that might perform a similar type of operation as you, and see how they went about storing the information. For example, if you find evidence where Apple is storing 100 64px x 64px thumbnail images in a single .plist file, then I would take that to mean that you'd probably be okay to do something comparable.

Hope this helps....

NSUserDefaults storage size capacity?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.