简体   繁体   中英

NSUserDefault memory management issue

I'm saving my class instance with some data (images, strings, arrays etc) inside to my NSUserDefaults using this code:

NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:myClassInstance];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedData forKey:@"myClassData"];
[defaults synchronize];

I'm doing this to use this data if I won't have internet connection.

Does all this data load to memory with NSuserDefaults? Or it loads when i use this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:@"myClassData"];

It will load when you call

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:@"myClassData"];

I am not sure when the data is loaded, but according to guidelines by Apple, you should not use nsuserdefaults to store any amount of data, as it is slow. You should just write the data as a file. NSDictionary and NSArray can write them self to file and read back directly.

You really should not do this. NSUserdefaults is only thought for doing preferences and settings, very small stuff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM