Re: [iOS] NSUserDefaults-backed Properties
Re: [iOS] NSUserDefaults-backed Properties
- Subject: Re: [iOS] NSUserDefaults-backed Properties
- From: Matt Neuburg <email@hidden>
- Date: Sat, 09 Oct 2010 11:24:14 -0700
- Thread-topic: [iOS] NSUserDefaults-backed Properties
On Fri, 8 Oct 2010 13:16:54 -0600, Carter Allen <email@hidden> said:
>Hello everyone!
>
>I'm working on cleaning up an app that was written by a colleague of mine
>a while ago, and one of the things that he did quite a bit was use
>"properties" (declared as @properties, with custom getters/setters) that
>are actually backed by NSUserDefaults. Seemingly, this was to provide
>excellent persistence - no matter how the app closed or crashed, it
>always remembered exactly what was going on. This is still important even
>with the advent of multi-tasking, as I am still targeting 3.x, as well as
>users without multi-tasking.
>
>Here's an example of one of his "properties":
>
>- (BOOL)gameShouldBeCounted { return [[NSUserDefaults
>standardUserDefaults] boolForKey:@"gameShouldBeCounted"]; }
>- (void)setGameShouldBeCounted:(BOOL)shouldBeCounted {
> [[NSUserDefaults standardUserDefaults] setBool:shouldBeCounted
>forKey:@"gameShouldBeCounted"];
> [[NSUserDefaults standardUserDefaults] synchronize];
>}
>
>Calling synchronize that often can't possibly be efficient, but it seems
>to be the only way to make sure that defaults actually get written right
>away. Does anyone have any recommendations of how to make this better? I
>was thinking about some sort of KVC/O system, where actual ivars were
>used and NSUserDefaultsController was set to observe them and update
>asynchronously, but I have no idea where to start with a system like that.
That wouldn't make any difference. The only way to guarantee that defaults
are written to disk right now is to call synchronize.
However, there is also such a thing as being too paranoid. In general,
just don't call synchronize; as long as your app exits in good order, your
defaults will be written to disk (and this is just as true in a
multi-tasking world). If you don't crash, you won't lose any data. If you
do crash, there's another problem and you should concentrate on fixing it.
m.
--
matt neuburg, phd = email@hidden, <http://www.apeth.net/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.apeth.net/matt/default.html#applescriptthings
_______________________________________________
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