In an iOS class that will not appear as a view e.g.
@interface MyDataClass : NSObject{}
Is there a method that can be overridden and is consistently called at the end of the classes’ execution/lifecycle similar to viewDidUnload or dealloc, that can call methods safely?
Alternatively how would one go about implementing a method that could recognise the completion of the useful lifespan of such a class?
If you mean a method that runs at the end of lifetime of the Class as a whole (not an instance), I wonder how is the runtime supposed to know when you are done using a class (you can create new instances at any time)? There is an
+initializemethod, but technically the class itself is available forever (until the program exits).If you mean the lifetime of an instance, the method you are looking for is
-dealloc.-dealloc is called whenever an object’s internal reference count reaches zero. In non-ARC code, if you override it you must call the superclass’ implementation, so that ultimately
NSObject‘s-deallocis called and that is when the memory is freed.EDIT: Regarding low memory situations, this is how you register for notifications:
Inside dealloc, you MUST do this:
…otherwise, your app may crash.
And of course, you must implement a method with the following signature that will be called on low-memory situations:
(otherwise, your app will crash)