Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3357676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:39:42+00:00 2026-05-18T02:39:42+00:00

I load from NSUserDefaults in my object’s init method. Can I save to NSUserDefaults

  • 0

I load from NSUserDefaults in my object’s init method.
Can I save to NSUserDefaults in my object’s dealloc method?

Something exactly like:

-(void)dealloc {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:self.filenamesArray forKey:self.defaultsKey];
    [userDefaults synchronize];
    self.filenamesArray = nil;
    self.defaultsKey = nil;
    [super dealloc];
}

good, bad, ok? if it’s not good, where would be better.

Edit:

Thanks for the detailed responses. All those things make sense. Another reason I discovered why this is a bad place to save to user defaults, is that dealloc is only called when an object deallocates nicely. If my app is killed, this code never runs. Similarly, if the app is pushed into the background (iOS 4) this doesn’t run.
I’ve also removed the explicit [userDefaults synchronize] call. It makes me a little nervous, but I put my trust in apple on this one. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:39:42+00:00Added an answer on May 18, 2026 at 2:39 am

    It’s bad because by the time -dealloc is called, other objects have given up interest in it. This can have lots of different implications.

    The better place to set user defaults is the very moment a setting changes. That way it’s persisted right away and the app can be killed with no worries about a setting not being persisted because parts of your object graph might already have gone away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I achieve this? I am trying to load content from the database
I am trying to load UIImage object from NSData , and the sample code
If I'm running a signed Java applet. Can I load additional classes from remote
I load my data from NSUserDefaults and it works well. But I'm a little
I'm trying to load an array from the NSUserDefaults in an iPhone application. I
I have some dylibs to load from python with ctypes. I can load libbass.dylib
I'm using the following html to load dojo from Google's hosting. <script src=http://www.google.com/jsapi></script> <script
I have a question about using streams in .NET to load files from disk.
I want to load some data from mysql into my cocoa application view before
Let me describe the behavior I get: Load a user from the database: this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.