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

  • SEARCH
  • Home
  • 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 3307924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:27:44+00:00 2026-05-17T21:27:44+00:00

According to NSObject’s documentation: Important: Note that when an application terminates, objects may not

  • 0

According to NSObject’s documentation:

Important: Note that when an
application terminates, objects may
not be sent a dealloc message since
the process’s memory is automatically
cleared on exit — it is more
efficient simply to allow the
operating system to clean up resources
than to invoke all the memory
management methods.

That’s fine, but if my objects needs to do something on dealloc, such as saving its state to disk or log something? How can I make sure that code gets called?

  • 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-17T21:27:45+00:00Added an answer on May 17, 2026 at 9:27 pm

    Persistency management should not be tied to dealloc. If you want to save object state, you should have some kind of session object that will collect the dirty objects and save the changes once in a while or when the application terminates/goes into background.

    An example using application settings: Let’s say you don’t want to use NSUserDefaults for you application’s settings, maybe because you have some extra logic to do. You have a Settings class that keeps all the settings and obviously you would like to keep the changes persisted.

    You could stuff all the persistency logic into the Settings class, but that violates the single responsibility principle. (= There are good reasons why this would cause you pain.) So you could add a Session class that will take of persisting the changes made in Settings.

    When the application starts you will create an instance of the Session and ask for Settings:

    Session *session = [[Session alloc] init];
    Settings *settings = [session loadSettings];
    

    Now if there is a file on the disk that contains saved settings, the session will load it (which is simple, as the Settings class implements NSCoding). If not, the session will create a fresh Settings instance and return that. In addition, the session will probably start watching for changes in the returned Settings instance, say using NSNotificationCenter. (It’s quite natural that a Settings object would fire a notification when the settings change.)

    Now when you change something inside a Settings instance that you got from the session, the session will notice that and it will save the changes to disk. Which should be trivial, since Settings implement NSCoding. You could also mark the object as dirty and only save the changes every few seconds, which is a better solution in case you update the object quite frequently. In that case you might also want to force-save the session when the application is going to terminate or is going background.

    I’m not saying this scenario is perfect, but it’s certainly better than objects that persist themselves in dealloc 🙂

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

Sidebar

Related Questions

According to this discussion , the iphone agreement says that it doesn't allow loading
According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to
According to Wikipedia, on the Comparison of programming languages page, it says that F#
According to the feedparser documentation , I can turn an RSS feed into a
According to documents I read, they always show that I should define a subview
According to PHP, locale information is maintained per process. Is my understanding correct that
According to Single-Threaded Application with Long-Running Calculation MSDN example, there is a possibility of
According to the MSDN documentation , transactional NTFS doesn't seem to allow one to
According to the documentation : Fills the internal buffer with the specified number of
According to the manual , git dcommit will create a revision in SVN for

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.