SuperString
SuperString

Reputation: 22519

iOS: unloading and popping view controllers

I have an application where there are a few view controllers on top of each other before the final stage where the user "saves" all the data he has entered in the stages. During the entering data phase, the user is allowed to go back and change some of the data. However I want to release some of the data structures because they consume a lot of memory. I can't release it during viewdidunload because it only gets called when the device is low on memory. I also can't release it in viewDidDisappear because the data wouldn't be there if the user wants to go back and change stuff.

I think the way to do this is to set up a pointer of the viewcontrollers to the previous viewcontrollers and then call a release function on the pointer to the previous viewcontroller to release stuff.

How do I do this?

I think I put something like

#import "SomeViewController.h"

@property SomeViewController someViewController;

in the .h file? I am not sure where to go from here.

Upvotes: 0

Views: 174

Answers (2)

andreamazz
andreamazz

Reputation: 4286

If you are using too much memory, the system will handle that for you, in a more efficient manner that any DIY system. That's why memory warnings, ARC, pools and all the other good stuff is there in the first place. So I would advise against what you're trying to accomplish, and just handle the memorywarnings accordingly. By the way, note that it's not a good practice to store data in your viewControllers, you should instead keep your data sctructures in separate objects (as MVC suggests). You can for instance keep your data in a singleton object, that you can access from every view controller, in this way you can also release the memory that you no longer need, whithout keeping pointers to all the VC's chain.

Upvotes: 2

Ravi
Ravi

Reputation: 8309

It depends on what kind of data you are trying to save. I would go up the hierarchy of data storage in iOS. See if NSUserDefaults would help you (which, from the nature of your question, I don't think would help). Then try SQLite. Then go for Core Data. Depending on the nature of data, maybe you can also try storing data in files. If you could have static methods in a separate class that handles saving all this intermediate data as you navigate the levels, it would be fantastic. Once you save the final data, delete all your temporary storage and you are done.

Oh, another advantage of using this technique is to "proceed from where you previously left off" in case the user, for some reason, decides to kill your app and re-launch it or your app unexpectedly crashed. In such a scenario, check until what level the navigation has been done and proceed from there.

Upvotes: 0

Related Questions