How to make sure your dealloc code gets called when the application terminates?

According to NSObject documentation:

Important: Please note that when the application terminates, objects may not be sent a dealloc message, since the process memory is automatically cleared when exiting --- more efficiently for the operating system to clear resources than invoke all memory management methods.

This is good, but if I need objects to do something on dealloc, for example, save its state to disk or write something? How can I make sure the code is called?

+3
source share
2 answers

dealloc. , - , / .

. , NSUserDefaults , , , . Settings, , , .

Settings, . (= , ). , Session, , Settings.

, Session Settings:

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

, , , ( , Settings NSCoding). , Settings . , , , Settings, , NSNotificationCenter. ( , Settings .)

, - Settings, , , . , Settings NSCoding. , , . , .

, , , , , , dealloc: -)

+7

, applicationWillTerminate: (NSApplicationDelegate).

+5

Source: https://habr.com/ru/post/1772101/


All Articles