Efficiency of loading Localizable.strings and NSUserDefaults
Efficiency of loading Localizable.strings and NSUserDefaults
- Subject: Efficiency of loading Localizable.strings and NSUserDefaults
- From: Development <email@hidden>
- Date: Wed, 15 Jul 2009 16:45:13 -0400
This is not a "help, the code doesn't work" question, but more a
general question to help one write better Cocoa code.
Imagine my program requires a large amount of localizable strings. In
many cases (ex. error codes), I have a string associated with an
integer value. For some of these, I could create an array of strings,
and when I need a specific String #18, I would go to the 18th element
of the array. For the sake of this discussion, I am using C array
structure, not NSArray objects.
I could just pre-load all the strings in at startup, so they are
always there. This takes a bit more memory and startup time, but has
the advantage that the strings are always there.
Or I could set the elements to the array to nil, and the first time I
need a specific string, I load that string from the
Localizable.strings file, use it, while setting the element of the
array to the string for future use. Thus once I use a string, it is
available for quicker access a second time.
Assume for this argument, that I am using writing some user interface
where I actually need the strings fast. Going to disk ever time is
not an option.
Either of these approaches can work, but they are based on the premise
that retrieving the string from the Localizable.strings file causes no
buffer of it's own?
Is this true? When I invoke -localizedStringForKey:value:table: (or
the other variations of NSLocalizedString), is the entire
Localizable.strings file read into memory? Is it thrown away at the
end of the call? Or is the localized version of all the strings kept
in memory for use? Is the entire file read into memory, or
If it is kept in memory, it makes no sense for me to store the strings
in an array. Disk access is slow, but extracting a specific string
from a block of data already in memory is fairly fast. In fact, when
I load in 10 strings from the Localizable.strings file, am I reading
the entire file in 10 times? I can not image this is the case, there
must be some buffering happening.
Anyone has some comments or ideas about this? Possibly someone with
some inner understanding of how localized strings are read?
Related to this idea, is the similar case of reading strings from and
to the NSUserDefaults object (preferences). Should I read all my
preferences (Strings, integers, boolean) in at startup? Or should I
read them when I need, assuming that Cocoa is keeping them in memory?
Apple has been recommending that applications save the preferences to
the NSUserDefaults object when the preferences get changed, and not
as shut down. This prevents loss of preferences due to crashes or
forced quits. Another reason was explained to me at WWDC. For the
iPhone, you do not want to take up any time, when your applications
switches out to another. Save the preferences when the user changes
them, you have the time then.
Again, anyone has thoughts or comments about the inner working of
NSUserDefaults?
Thank you!
Steve Sheets
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden