Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Core Data is not a relational database." Why exactly is this important to know?

I realize this may be common sense for a lot of people, so apologies if this seems like a stupid question.

I am trying to learn core data for iOS programming, and I have repeatedly read and heard it said that Core Data (CD) is not a relational database. But very little else is said about this, or why exactly it is important to know beyond an academic sense. I mean functionally at least, it seems you can use CD as though it were a database for most things - storing and fetching data, runnings queries etc. From my very rudimentary understanding of it, I don't really see how it differs from a database.

I am not questioning the fact that the distinction is important. I believe that a lot of smart people would not be wasting their time on this point if it weren't useful to understand. But I would like someone please to explain - ideally with examples - how CD not being a relational database affects how we use it? Or perhaps, if I were not told that CD isn't a relational database, how would this adversely impact my performance as an Objective-C/Swift programmer?

Are there things that one might try to do incorrectly if they treated CD as a relational database? Or, are there things which a relational database cannot do or does less well that CD is designed to do?

Thank you all for your collective wisdom.

like image 888
TJ Rogers Avatar asked Oct 28 '14 01:10

TJ Rogers


People also ask

Is Core Data a relational database?

Core Data is not a relational database or a relational database management system. It can use SQLite as one of its persistent store types, but it is not in and of itself a database. You could set up Core Data to just use an in-memory store just to get the change tracking and management features without persistence.

Why are non-relational databases useful?

Non-relational databases are therefore ideal for storing data that may be changed frequently or for applications that handle many different kinds of data. They can support rapidly developing applications requiring a dynamic database able to change quickly and to accommodate large amounts of complex, unstructured data.

What is the purpose of 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.

Is Core Data a database?

Core Data is not a database. Core Data is a framework for managing an object graph. An object graph is nothing more than a collection of interconnected objects. The framework excels at managing complex object graphs.


2 Answers

People stress the "not a relational database" angle because people with some database experience are prone to specific errors with Core Data that result from trying to apply their experience too directly. Some examples:

  • Creating entities that are essentially SQL junction tables. This is almost never necessary and usually makes things more complex and error prone. Core Data supports many-to-many relationships directly.
  • Creating a unique ID field in an entity because they think they need one to ensure uniqueness and to create relationships. Sometimes creating custom unique IDs is useful, usually not.
  • Setting up relationships between objects based on these unique IDs instead of using Core Data relationships-- i.e. saving the unique ID of a related object instead of using ObjC/Swift semantics to relate the objects.

Core Data can and often does serve as a database, but thinking of it in terms of other relational databases is a good way to screw it up.

like image 178
Tom Harrington Avatar answered Oct 13 '22 17:10

Tom Harrington


Core Data is a technology with many powerful features and tools such as:

  1. Change tracking (undo/redo)
  2. Faulting (not having to load entire objects which can save memory)
  3. Persistence

The list goes on..

The persistence part of Core Data is backed by SQLite, which is a relational database.

One of the reasons I think people stress that Core Data is not a relational database is because is it so much more than just persistence, and can be taken advantage of without using persistence at all.

By treating Core Data as a relational database, I assume you mean that relationships between objects are mapped by ids, i.e. a Customer has a customerId and a product has a productId. This would certainly be incorrect because Core Data let's you define powerful relationships between object models that make things easy to manage.

For example, if you want to have your customer have multiple products and want to delete them all when you delete the customer, Core Data gives you the ability to do that without having to manage customerIds/productIds and figuring out how to format complex SQL queries to match your in-memory model. With Core Data, you simply update your model and save your context, the SQL is done for you under the hood. (In fact you can even turn on debugging to print out the SQL Core Data is performing for you by passing '-com.apple.CoreData.SQLDebug 1' as a launch argument.

In terms of performance, Core Data does some serious optimizations under the hood that make accessing data much easier without having to dive deep into SQL, concurrency, or validation.

like image 35
Krys Jurgowski Avatar answered Oct 13 '22 17:10

Krys Jurgowski