1

I am using NSCoding to save and restore my view controllers. However I'm saving to disk the navigation stack + view controllers every time a view controller is pushed or popped.

This is not energy efficient, and there's a better way.

Think of NSUserDefaults. It saves the changes somewhere in memory (and if called repeatedly as it is, just overwrite) and when the synchronize method is called it writes them to disk. This is genuinely done, and is super energy efficient.

So can I implement something like this? On every call to save changes somewhere in memory and when a synchronize/writeToDisk method is called to purge the memory cache and write them to disk. Any ideas will be greatly appreciated!

My idea is to use NSCache, and in application:willResignActive or application:willTerminate to get the object in the cache and write it do disk.

Thanks so much!

Devfly
  • 2,495
  • 5
  • 38
  • 56
  • Can I ask why you are doing this? Wouldn't it be much less memory/processing to save the datamodel the drives the view controllers, instead of the viewcontroller stack itself? That way you can just recreate it/them instead of serializing the entire controller stack. – Michael Kernahan Oct 30 '12 at 16:04
  • Interesting option ahead, buddy. By using NSCoding is the easiest option I found, but this is pretty interesting. I'll try using your approach. Any ideas on caching this data model? – Devfly Oct 30 '12 at 16:13
  • `NSCoding` is likely what you'll want. Basically make it so that you can (de)serialize your objects and then use `[NSKeyedUnarchiver unarchiveObjectWithFile:path];` and `[NSKeyedArchiver archiveRootObject:obj toFile:path]` – Michael Kernahan Oct 30 '12 at 17:25

0 Answers0