Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCoding VS Core data

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?

like image 950
LolaRun Avatar asked Mar 05 '12 15:03

LolaRun


People also ask

When should I use Core Data vs Userdefaults?

Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

What is meant by Core Data?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

When should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is NSCoding protocol?

The NSCoding protocol declares the two methods that a class must implement so that instances of that class can be encoded and decoded. This capability provides the basis for archiving (where objects and other structures are stored on disk) and distribution (where objects are copied to different address spaces).


4 Answers

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.

like image 117
MrTJ Avatar answered Jan 10 '23 16:01

MrTJ


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

like image 25
Piper Avatar answered Jan 10 '23 15:01

Piper


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.

like image 33
Clay Avatar answered Jan 10 '23 17:01

Clay


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.

like image 41
Au Ris Avatar answered Jan 10 '23 17:01

Au Ris