Last week I asked on Twitter why people choose Realm over Core Data. The answers are both surprising and interesting.

I would like to dive a little deeper into the comparison between Core Data and Realm. While I have used Core Data for close to ten years, I don't have much experience with Realm. With this article, I would like to debunk a few misunderstandings about Core Data and point out some less obvious advantages of Core Data and Realm. I don't intend to promote or favor one or the other. That is not the goal of this article.

Core Data Is Misunderstood

It is a fact that many developers don't take the time to learn the basics of the Core Data framework and this inevitably leads to problems and frustration. Core Data isn't Foundation or UIKit. You cannot learn the framework as you go. You need to understand what makes the framework tick before you can use it in a project.

I firmly believe this is the primary reason the framework has a bad reputation among developers. For example, if you don't know how Core Data should be used in a multithreaded environment, you will undoubtedly run into problems you didn't expect. But the same is true for other frameworks and libraries.

Difficult to Set Up

The Core Data stack is the first concept developers need to wrap their heads around and it is probably the most difficult concept to grasp. Admittedly, it can be a tough hurdle to cross.

But once you understand what the persistent store coordinator is and how it relates to the managed object context, the puzzle becomes less ... well ... puzzling.

It is clear the Core Data team at Apple understands the problem. With the introduction of the NSPersistentContainer class, setting up the Core Data stack becomes trivial.

let persistentContainer = NSPersistentContainer(name: "Notes")

Even though this doesn't mean developers don't need to learn about the inner workings of the Core Data stack, it simplifies the setup for many types of applications. It avoids a lot of boilerplate.

Threading

It is true that Core Data is expected to be run on a single thread, but it performs great in a multithreaded environment. I agree that it used to be a nightmare to use Core Data in multithreaded applications, but that is a problem of the past. In fact, thread confinement, which used to be the recommended approach, is deprecated as of iOS 9.

As long as you play by the rules of the framework, using the framework in a multithreaded environment isn't rocket science. Each managed object context is associated with a dispatch queue on which it performs its work. As long as you use the perform(_:) and performAndWait(_:) API, you are safe.

managedObjectContext.perform {
    ...
}

An important advantage Realm has over Core Data is consistency across threads. An important downside of Core Data is that managed object contexts can get out of sync. This isn't true for Realm and that is a key advantage it has over Core Data.

Syntax

Working with Core Data used to be verbose and it is the framework's verbose API that led to the emergence of many, many third party libraries over the years. This is no longer true, though.

If you are using Core Data in combination with Swift 3 and Xcode 8, then you have a powerful combination at your disposal. For example, the recent introduction of the NSFetchedResultsType protocol makes working with Core Data elegant and concise. Creating a managed object for an entity no longer requires multiple lines of code.

let note = Note(context: managedObjectContext)

The API is concise and to the point. In the example, we create an instance of the Note class, a NSManagedObject subclass, and insert it into a managed object context.

The syntax for creating and executing a fetch request is short and focused.

let fetchRequest: NSFetchRequest<Note> = Note.fetchRequest()

managedObjectContext.perform {
    do {
        let notes = try fetchRequest.execute()
    } catch {
    	...
    }
}

Migrations

Realm is still very young while Core Data has been around for more than a decade. The result is that Core Data has a few bells and whistles Realm lacks. Xcode's data model editor, for example, is a feature that is often overlooked. For complex applications, it is an incredibly useful tool to have.

Another powerful feature of Core Data is data model versioning and the framework's support for migrations. Versioning the data model is easy and lightweight migrations are handled by the framework.

Even though Realm also supports migrations, the developer needs to do the heavy lifting. I am sure this will improve over time.

Speed

Core Data is incredibly fast if you consider what it does under the hood to do its magic. But Realm is faster, much faster. Realm was built with performance in mind and that shows.

As Marcus Zarra once said in a presentation, you don't choose Core Data for its speed. But if speed is a requirement, then you are better off with Realm.

Cross-Platform Support

Realm is available on multiple platforms and this can be a compelling reason for choosing Realm over Core Data. I agree that it is nice to use the same persistence solution on multiple platforms. But keep in mind that you still need to write an implementation for each platform. You cannot share code between platforms, unless you use Xamarin, which is another discussion.

Another compelling feature is the recently announced Realm Mobile Platform. Not only does it look amazing, it offers a valid alternative to, for example, iCloud and Firebase.

But Apple understands the need for cross-platform compatibility, which is the reason for the company's CloudKit web services. One of the key differences with Realm's solution is that Apple is in charge of the user's data and the server infrastructure. While this is great for many developers and companies, it is unacceptable for others.

Evolution

Realm is a third party solution while Core Data isn't. This has pros and cons. Realm can evolve at a rapid pace. Developers can take advantage of new features by upgrading the version of Realm they ship with their application.

This isn't true for Core Data. As a developer, you can't decide which version of the framework your application uses. This means that adopting new features of the framework isn't trivial. Apple is in charge.

And that brings us to support. If you find a bug in Core Data, you can file a bug report and hope the bug is fixed in a future release. The same is true for feature requests. Core Data has been around for more than a decade. The NSPersistentContainer class should have been added to the framework many, many years ago.

The reason Realm moves at such a swift pace is that the company listens to its users. The one advantage Core Data has over Realm is its maturity. It doesn't need to evolve as fast as Realm does. That said, Realm improves fast and it won't need ten years to be on par with Core Data.

Core Data or Realm

There currently isn't a clear winner. Much depends on the needs of your project. Realm is still young, but it evolves at an astounding pace. If your project requires encryption or speed, then Realm is an obvious choice. If your project has a complex data model that changes frequently, then Core Data might be a better choice.

I am curious to hear your experiences with Core Data and Realm. Why did you choose one over the other? Do you regret choosing one or the other?