What Is the Difference Between Core Data and SQLite

What is the difference between Core Data and SQLite? Even though it's a common question, it is the wrong one to ask. Comparing Core Data and SQLite is like trying to compare apples and oranges. But why is that?

The short answer is simple. Core Data is a framework for managing an object graph. SQLite is a relational database. Continue reading if you are interested in the long answer.

What Is SQLite

SQLite is a library that implements a lightweight database engine that is incredibly performant and, therefore, a great fit for embedded systems, such as mobile devices. Even though SQLite is advertised as a relational database, it is important to realize that the developer is in charge of maintaining the relationships between records stored in the database.

Like many other databases, SQLite stores data and that is what it is good at. The SQLite website claims its the most used database in the world.

What Is Core Data

The most important difference between Core Data and SQLite is that SQLite is a database while Core Data is not. That is the most important difference because there is very little to compare. Core Data and SQLite are solutions to different problems.

Core Data can use SQLite as its persistent store, but the framework itself is not 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.

Core Data manages an object graph.

The framework is responsible for managing the life cycle of the objects in the object graph. It can optionally persist the object graph to disk and it also offers a powerful interface for searching the object graph it manages.

But Core Data is much more than that. The framework adds a number of other compelling features, such as input validation, data model versioning, and change tracking.

Persistent Store Types

I already mentioned that Core Data can use a SQLite database as its persistent store, but it also has support for other persistent store types, including a binary store and an in-memory store.

Even though Core Data knows how to use a SQLite database as its persistent store, that doesn't mean you can hand it any SQLite database. The database schema of the SQLite database used by Core Data is an implementation detail of the framework. It isn't publicly documented and liable to change.

Core Data or SQLite

What should you use? Core Data or SQLite? Once again, that is the wrong question to ask. If you need a lightweight solution and don't need Core Data's feature set, then SQLite may fit your needs. If you don't like C, then you may want to consider Gus Mueller's FMDB library. It provides an object-oriented wrapper written in Objective-C.

But if you are managing a complex object graph with many entities, attributes, and relationships, then Core Data is definitely worth considering. Getting up to speed with Core Data is easier than you might think.

If you decide to opt for Core Data, make sure you don't skip the basics of the framework. Core Data has a learning curve, but it isn't as bad as many make you believe it is.

If you want to learn more, then you should check out Mastering Core Data With Swift. In this course, you learn everything you need to know to integrate Core Data in your next Swift project.