BSD

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Saving user defaults before program ends

    5 answers - 1860 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    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
    :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
    BL monitoring = ( [enabledButton state] == NSState ? TRUE :
    FALSE );
    [defaults setBool:monitoring forKey:@"enabled"];
    // Device
    if( device ) {
    deviceAsData = [NSKeyedArchiver archivedDataWithR:device];
    [defaults :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 (Cocoa-dev (AT) lists (DOT) apple.com)
    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:
    %40developershed.com
    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.1 | | 676 bytes | |

    You should give this document a read:

    "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
  • No.2 | | 1192 bytes | |

    That was it I've read the documentation for NSUserDefaults several
    times and missed this key part:
    - (BL)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!

    Jan 16, 2007, at 5:18 PM, I. Savant wrote:

    You should give this document a read:

    "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
  • No.3 | | 565 bytes | |

    That was it I've read the documentation for NSUserDefaults
    several times and missed this key part:

    It always helps to be thorough. I knew the method name, but only
    searched the docs for the user defaults topic in general. It took
    only a few seconds from there to find a relevant-sounding subheading.

    When the class reference for an object doesn't make a given
    subject clear, it's always good to read the associated topic
    documentation (if it exists -- it usually does) to get a better feel
    for the concepts involved.
  • No.4 | | 5055 bytes | |

    , 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.
    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.

    That was it I've read the documentation for NSUserDefaults several
    times and missed this key part:
    - (BL)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!
    --
    Jan 16, 2007, at 5:18 PM, I. Savant wrote:
    >
    >>

    >You should give this document a read:
    >>

    >
    >
    >>

    >"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.
    >>
    >>

    >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 :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
    BL monitoring = ( [enabledButton state] == NSState ? TRUE :
    FALSE );
    [defaults setBool:monitoring forKey:@"enabled"];

    // Device
    if( device ) {
    deviceAsData = [NSKeyedArchiver archivedDataWithR:device];
    [defaults :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 (Cocoa-dev (AT) lists (DOT) apple.com)

    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 idiotsavant2005 (AT) gmail (DOT) com
    >>

    >


    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)

    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:
    %40tbef.se

    This email sent to tage (AT) tbef (DOT) se

    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)

    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:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com
  • No.5 | | 5387 bytes | |

    Yep, it works. Thanks for the suggestions about MVC'll definitely
    keep them in mind when I start implementing some of this proof-of-
    concept stuff in my larger program.

    Denver

    Jan 17, 2007, at 4:48 AM, Tage Borg wrote:

    , 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.
    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.


    >
    >That was it I've read the documentation for NSUserDefaults several
    >times and missed this key part:
    >- (BL)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!
    >>
    >>

    >Jan 16, 2007, at 5:18 PM, I. Savant wrote:
    >>


    You should give this document a read:

    "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.

    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 :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
    BL monitoring = ( [enabledButton state] == NSState ? TRUE :
    FALSE );
    [defaults setBool:monitoring forKey:@"enabled"];

    // Device
    if( device ) {
    deviceAsData = [NSKeyedArchiver
    archivedDataWithR:device];
    [defaults :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 (Cocoa-dev (AT) lists (DOT) apple.com)

    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 idiotsavant2005 (AT) gmail (DOT) com

    >>

    >
    >>

    >Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)
    >>

    >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:
    >%40tbef.se
    >>

    >This email sent to tage (AT) tbef (DOT) se
    >>

    >


    Cocoa-dev mailing list (Cocoa-dev (AT) lists (DOT) apple.com)

    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:
    %40developershed.com

    This email sent to bsdarchive (AT) developershed (DOT) com

Re: Saving user defaults before program ends


max 4000 letters.
Your nickname that display:
In order to stop the spam: 4 + 3 =
QUESTION ON "BSD"

EMSDN.COM