As a newcomer to iOS, I found that ARC has thus far really helped with rapid development. That being said, there are a few caveats where I am unsure how ARC fits and with previous longstanding iOS patterns, such as MVC. Here’s how I’m trying to picture things, and any clarification or corrections in my understanding would be greatly appreciated.
I’m using Storyboard to instantiate and setup some view controllers for my app. From the docs I’ve read, when the active view controller segues to a different view controller, the sending view controllers instance variables are lost (yes?/no?). As such, I’m using a model layer to store data for the app. Now here wheres my understanding starts to get (more) fuzzy..
Say I have a model data object in use by only one view controller. I allocate and instantiate the data object from within the view controller (in viewDidLoad). So now I have a reference to the data object from within my controller and while the controller is in the foreground, can freely coordinate information from views, to the controller, possibly to the data object, back to the controller to update the views using the MVC pattern. OK, cool..
But when I segue to a different view controller, the sender controller’s instance variables are lost and thus the only reference to the data object is lost. Does ARC then automatically release the data object too?
My goal is to be able to save data for a particular view controller so that when I return to it I can update it with the saved state. How is this done with MVC and ARC in iOS? Options I’ve thought of (remember, I’m new lol)
- Include a reference to the data object in the Application Delegate. It’s not going anywhere so the reference to the data object won’t either. Is this good precise?
- Store all data in static form in the data class object?
- Use NSUserDefaults
Any advice would be greatly appreciated going forward. Thanks for your time.
If you’re using, for example, a push or modal segue, the view controller of the previous view is not released. If you ever receive a
didReceiveMemoryWarning, the view, itself might be released, but the controller will not. You controller’s ivars will be preserved, so when the secondary view controller is popped off (or dismissed), your original ivars will be as they were before.