Re: Saving user defaults before program ends
Re: Saving user defaults before program ends
- Subject: Re: Saving user defaults before program ends
- From: "Tage Borg" <email@hidden>
- Date: Wed, 17 Jan 2007 12:48:44 +0100 (CET)
Ok, so does it work now, then?
Personally, I would have suspected the fact that you use the view as your
model for settings as the culprit. What if enabledButton has been
deallocated before the call to it is made? I would sugges (regardless of
whether -synchronize solves the immediate problem or not) that you use MVC
for you settings, i.e. store the value of enabledButton in a model object
and query that object instead of the querying the button itself. Or
perhaps even better: update the user defaults whenever the button state
changes and not just when the application is about to exit. Then just call
-synchronize in -windowWillClose:. The same probably applies to whenever
device changes.
--Tage
> That was it... I've read the documentation for NSUserDefaults several
> times and missed this key part:
> - (BOOL)synchronize
>
> Because this method is automatically invoked at periodic intervals,
> use this method only if you cannot wait for the automatic
> synchronization (for example, if your application is about to exit)
> or if you want to update the user defaults to what is on disk even
> though you have not made any changes.
>
> Thanks!
>
>
> On Jan 16, 2007, at 5:18 PM, I. Savant wrote:
>
>>
>> You should give this document a read:
>>
>> http://developer.apple.com/documentation/Cocoa/Conceptual/
>> UserDefaults/Tasks/UsingDefaults.html
>>
>> "In applications in which a run-loop is present, synchronize is
>> automatically invoked at periodic intervals. Consequently, you
>> might synchronize before exiting a process, but otherwise you
>> shouldnt need to."
>>
>> Specifically, you can call -synchronize to write the defaults to
>> disk, and you probably should since your app is in the process of
>> quitting when -windowWillClose: is called. It might just not be
>> getting a chance to call -synchronize.
>>
>> Though I really don't know *for sure* that this is what's
>> happening, a quick trip to the docs suggests you should try calling
>> -synchronize ...
>>
>> --
>> I.S.
>>
>>
>> On Jan 16, 2007, at 7:03 PM, Denver Timothy wrote:
>>
>>> Hi All,
>>>
>>> I have created a preferences window and successfully made my
>>> AppController class the delegate for the window and which
>>> implements the windowWillClose method. The windowWillClose method
>>> calls [self saveUserDefaults]. In saveUserDefaults, I have two
>>> NSLog calls, one at the top and one at the bottom, and then all
>>> the [defaults setObject:forKey] calls in between. Both NSLog() are
>>> executed as expected.
>>>
>>> The problem is that if the program is terminated (eg. Apple-Q),
>>> _none_ of the defaults are saved, but the two NSLog() calls _are_
>>> executed as expected. However, if the the window is simply closed,
>>> the defaults are saved as expected.
>>>
>>> Here is the relevant code:
>>>
>>> - (void)windowWillClose:(NSNotification *)aNotification
>>> {
>>> [self saveUserDefaults];
>>> }
>>>
>>> - (void)saveUserDefaults
>>> {
>>> NSLog( @"Saving user defaults" );
>>>
>>> NSUserDefaults *defaults;
>>> NSData *deviceAsData;
>>>
>>> defaults = [NSUserDefaults standardUserDefaults];
>>>
>>> // Enabled
>>> BOOL monitoring = ( [enabledButton state] == NSOnState ? TRUE :
>>> FALSE );
>>> [defaults setBool:monitoring forKey:@"enabled"];
>>>
>>> ...
>>>
>>> // Device
>>> if( device ) {
>>> deviceAsData = [NSKeyedArchiver archivedDataWithRootObject:device];
>>> [defaults setObject:deviceAsData forKey:@"device"];
>>> }
>>>
>>> [self stopMonitoring];
>>> [self startMonitoring];
>>>
>>> NSLog( @"Done saving defaults" );
>>> }
>>>
>>> Am I doing this properly? Should I be doing something different
>>> entirely?
>>>
>>> Thanks,
>>>
>>> Denver Timothy
>>>
>>> _______________________________________________
>>>
>>> Cocoa-dev mailing list (email@hidden)
>>>
>>> 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:
>>> 40gmail.com
>>>
>>> This email sent to email@hidden
>>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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