NSCoding / NSKeyedArchiver

Among the most important architectural decisions made when building an app is how to persist data between launches. The question of how, exactly, to re-create the state of the app from the time it was last opened; of how to describe the object graph in such a way that it can be flawlessly reconstructed next time.

On iOS and OS X, Apple provides two options: Core Data or NSKeyedArchiver / NSKeyedUnarchiver (which serializes NSCoding-compliant classes to and from a data representation).

nshipster.com/nscoding/

When you build an app, a key choice is how to save data between uses. You need to rebuild the app's state from where it left off. Apple gives options like Core Data or NSKeyedArchiver with NSKeyedUnarchiver. These turn objects into data and back again. For apps that fetch data from servers, NSURLCache can help by storing responses for quick reloads.

Core Data handles complex data well. It models entities, runs queries, and manages changes. But not every app needs that. Some have simple data that does not require queries or big shifts.

NSKeyedArchiver wins on ease. Classes that follow NSCoding can save and load with little work. You code initWithCoder and encodeWithCoder for each property. It suits apps with basic needs.

Save data to files or NSUserDefaults. Files work for lists of objects. NSUserDefaults fits small items like user details.

Pick the right tool. Core Data suits big tasks. NSKeyedArchiver keeps things simple. Start basic and add complexity as you need it.


Category:

Year: