25

I've been searching for an article that explains NSCoding (NSKeyedArchiver...) advantages and disadvantages over use of CoreData (SQLite....).

There's a lot of options, I can implement my own custom binary reader/writer, or use plists/xml/json... or use SQLite or NSCoding.

I'm kind of lost right now. Can any body explain what's the difference between the MAIN features?

3

4 Answers 4

35

It depends which kind of data you want to save and whether you will use it only internally or you have to exchange the data with an external service.

NSCoding is generally speaking a data serializer. A lot of built-in objects implements the NSCoder protocol that allows you to save them as a binary stream (file, in a BLOB of an sqlite, etc.) The NSKeyedArchiver gives you the plus of searching in such streams based on a string label, a bit like a dictionary but you can use only strings as keys. This approach is good if you occasionally have to persist some objects of different classes.

However if you have many objects of the same class, you'll better go for a database-approach, SQLite or CoreData. CoreData is practically a wrapper around SQLite that eases a lot designing your data model, and does the queries to the DB behind the curtains, without the need of writing SQL statements. In CoreData you define your classes, and each instance of the class can be persisted i.e. you can get back the values of the members of the object without having them always in the memory. This is a very convenient way to store a lot of structured data. For example if you'd write a web browser, you could store the bookmarks of the user with the name, URL, and maybe last visited time.

For XML and JSON there're no particular advantage if you use the data only locally to the device. If you have to communicate with some external service, you might consider to cache/save the XML / JSON objects as they are for later use. Other approach would be to regenerate this data from your internal data structures (see above) every time you need it.

If you design your data model yourself, I see even less point to use plists, but maybe somebody will correct me.

EDIT: I add here a short link reference for tutorials on how to use NSCoding, Core Data, and as a bonus, SQLite.

UPDATE 12.01.2016: If you are looking for persistence solutions I suggest you to also check out Realm.

6
  • if the blob files of an sqlite are serialized, that could mean that serialization is the best in order to obtain the smallest chunks of binary data. As well you can use NSCoding for conditionalObjectEncodings, which removes the duplication of in-memory alive object instances. The reason i'm asking this question, is because everybody talks and suggests Coredata-SQLite, but i'm not seeing THAT advantage Unless building against a huge datasource, and you can't put all that in memory. Am i right?
    – LolaRun
    Mar 5, 2012 at 18:12
  • 2
    There is always a balance between memory and performance optimization. With NSCoding, or your custom binary representation you have much more control over the bits being written to the storage or being stored in the memory, but for this you have to pay the price of coding more thus the efficiency of the code will depend on your abilities. With CoreData/SQLite a lot of useful things are implemented for you in an efficient way (searching, querying, indexing, joins, etc). Note: CoreData objects are loaded into the memory only when needed, otherwise they stay in the db.
    – MrTJ
    Mar 6, 2012 at 8:26
  • Excellent. So if i'm using "ApplicationDocument" template of applications, and if i need to save and open documents, i wouldn't really need the benefits of CoreData, since i need to load everything in memory, and don't need to query and... database functionalities. The major difference is the development cost. Which in my case is not a setback. Thanks a lot. I'll wait a bit, to attract more answers and more discussions, before accepting an answer
    – LolaRun
    Mar 6, 2012 at 13:41
  • 1
    Yes, developing your own (binary) document format is a typical case to go for the more low level, serialization type tools like the NSCoder framework.
    – MrTJ
    Mar 6, 2012 at 13:45
  • I know this is an older thread, but I thought I would add some insight. Honestly, I would throw in simplicity and security as keypoints as well.
    – Clay
    Aug 14, 2013 at 20:13
9

Mattt Thompson provides a digestible breakdown of various differences among NSCoding, Core Data, and NSKeyedArchiver on NSHipster: http://nshipster.com/nscoding/

0
3

There's always an impedance between objects and relational structures. I always prefer objects since data access is typically a fraction of functionality in your app. With NSCoding, you get simplicity, ease of debugging, and control with very little code to write anyways. You also have the flexibility to incorporate NSCoding into your database structures.

NSCoding is a persistence mechanism for saving objects. If you add indexes to your relational structures for search optimization and maintain object structures, then I think you get the best of all worlds at a very low cost and ease of maintenance.

2

To add to already great answers, NSCoding along with NSKeyedArchiver is a great way to store data which is too big (or incompatible data type) for for NSUserDefaults but too small and non-numerous for CoreData.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.