Search code examples
iphoneiosobjective-cdealloc

Write to file in dealloc method


I'm writing an iOS app where my main 'UIViewController' creates a model (subclass of NSObject). When I am done using the model I want the model to write some data to file. Would it be appropriate/correct for me to do this file writing in the 'dealloc' method of my model? I want this file writing to always happen, even if a user exits the app.


Solution

  • Please don't do this - the dealoc(see here) method should release memory(and writing to file/save is the opposite of this) and you can't control when it will be called(Maybe the user quits the app and dealoc wasn't called a single time). You can create a costum method which gets called when you are done with your model object. If you want to make sure that the data will be saved when the user quits the app write your save method in - (void)applicationDidEnterBackground:(UIApplication *)application