Realm is an Object-Centric Modern Database for Mobile Apps

Realm Database series header

The Realm Database is a new kind of mobile database that’s been built from the ground up to power present-day applications.

It’s an easy mistake to think that Realm is built on top of an existing technology, or that it’s just a modern rewrite of another library. After all, unlike with server databases, there hasn’t been much innovation on the client side.

In this series, we’re going to look into few aspects which set Realm apart from other technologies. If you’ve heard of Realm but don’t know much about what makes it special, these posts are for you.

Find out why companies like these and thousands of others trust Realm to power their apps for more than 1 billion users.

Get more development news like this

Logos of companies using Realm, including Amazon, Google, Hipmunk, Starbucks, and more

Data Layer Libraries and ORMs

A lot of what you would call “database libraries” are simply that — third party libraries using a particular storage engine that provide a modern/functional/different (pick your favorite) approach to accessing an existing data layer.

You could use a Swift or Java library to access your SQLite storage in a more modern way by using generics and structs. But the storage engine that’s doing all of the reading and writing to disk is still SQLite.

Plus, that third-party library you use will still need to convert your native data structures to some intermediate format, execute behind-the-scenes SQL queries, and convert your data to SQL table rows.

If you are using a full-fledged ORM, you will almost always have numerous operations running in the background. An ORM will continuously convert your objects to intermediate format and run SQL queries to talk to a SQLite file. Any time you access a related object to the object you’re working with - the ORM will convert this to a JOIN SQL query, execute that query on a lookup table, and find matching records in another table where those related objects are stored.

Objects vs. Tables

This is a lot of work for simple data access. Not only does it take up CPU cycles and disk time, but it can become slow very quickly. That means you have to take that work off your main thread and introduce proper threading techniques, and now your code is getting rather complicated, just to get your data.

Realm Isn’t Reiterating Existing Tech

Considering the existing solutions and the shortcomings mentioned above, the Realm team went for creating their own, built-for-performance storage engine.

The Realm Database therefore isn’t related to SQLite or another SQL database; it aims instead to solve many of the issues that exist in that field. But the Realm Database isn’t a key-value store either — this kind of storage is great in certain situations, but developers really want to be able to work directly with native objects in their code.

Finally, the Realm Database isn’t an ORM. ORMs convert flat data like SQL database tables into object graphs, so that those can be used from native code. Realm’s custom storage engine doesn’t need to convert data to and from object graphs, so there’s no need for an ORM.

The Realm Database persists objects directly on disk with the least possible type or structure conversion. Since there’s no mapping or other untangling of complex entities on-the-fly, the Realm Database is able to get objects from memory to disk extremely quickly.

In fact, that’s one of the trademark features of the Realm Database: You are welcome to read and write on the main thread! You don’t have to be afraid that this will block your UI. (Within reason of course, we’re not magicians. 🎩)

The Realm Database outperforms competing technologies

Realm Core is written in super-fast C++, and it maps C++ objects to disk. Since it’s developed alongside the SDKs, its API and behavior are closely aligned to the ways that mobile apps actually use data, so you get an easy mental model of how it works.

This tight alignment is reminiscent of iOS: Apple makes the hardware for the iPhone and also provides the developers with the best SDK to make most of that hardware. That’s why scrolling through a long contact list on an iPhone never drops frames, even if the hardware isn’t as powerful as what other devices have.

Further, by sporting a solid, compatible, and multi-platform C++ core, Realm has the potential to run pretty much anywhere, and is not restrained to a certain platform or vendor.

You might be starting to get worried about all this C++ talk, and you might even be asking, “So, do I have to know C++ to use Realm?”. Thankfully, you don’t have to, because…

A Solid Core That Speaks Your Language

The Realm Database supports five SDKs, very thin wrapper APIs that give you access to the underlying Core API from mobile programming languages.

Realm SDKs: Java, Objective-C, React Native, Swift, Xamarin

Each of the SDKs is designed to use the language and platform features that developers are used to. Let’s look at the basics in Realm Swift and Realm Java:

To start with Realm Swift, you might define an object:

class Repository: Object {
  dynamic var id: Int = 0
  dynamic var stars: Int = 0
  dynamic var url: String = ""

  dynamic var name: String?
  dynamic var favorite: Favorite?

And then query for some of those objects:

let repos = realm.objects(Repository.self)
  .filter("name contains[c] %@", searchTerm)

The code makes use of Swift’s generics to strongly type the result set, and it uses Apple’s NSPredicate to allow you to query by using a syntax you’re accustomed to. A functional style chaining of methods is pretty popular with Swift programmers, using methods like map, filter and sort on the standard collection types, so Realm’s collection types expose similar APIs.

Now let’s look at the same concepts in Realm Java. You can define the same object in Java:

public class Repository extends RealmObject {
  private Int id;
  private Int stars;
  public Int getId() { return id; }
  public void setId(Int id) { = id; }
  public Int getStars() { return stars; }
  public void setStars(Int id) { this.stars = stars; }


Then you query those Repository objects on Android:

RealmResults<Repository> results = realm.where(Repository.class)
  .contains("name", searchTerm).findAll();

This Java code does the same things that the Swift code earlier did. But the API isn’t identical to the Swift SDK. This code fits much better in the rest of the codebase of an Android app, because it follows the usual Android style.

Full-Fledged Classes

Last but certainly not least, Realm objects are full-fledged classes. You can use all the tools your favorite programming language offers to make them do what you need, fast!

You can build features ranging from simple computed properties that automatically format values:

class Repository: Object {
  dynamic var stars: Int = 0

  var starsDecorated: String {
    return "\(stars) ⭐️"

All the way up to encapsulating business logic:

extension Repository {
  static func all(searchTerm: String? = nil) -> Results<Repository> {
    let realm = try! Realm()
    return realm.objects(Repository.self)
      .filter("name contains[c] %@", searchTerm ?? "")
      .sorted(byProperty: "stars", ascending: false)

If you want to have a look at a full fledged Realm object check the companion project for this article on GitHub.

The example Repository class features:

  • Simple data properties (id, stars)
  • Related objects (favorite)
  • Computed properties (nameDecorated)
  • Dynamic properties (avatarUrl)
  • Custom init
  • Indexes (primaryKey(), indexedProperties())
  • Metadata (ignoredProperties())
  • Simple entity methods (all(searchTerm:), add(repos:), and toggle(favorite:))

We Hope You Like What You’re Seeing!

We’ve just touched upon some of the great features of the Realm Database. Its custom database engine gives it a solid boost in performance and robustness.

This post was just a tad on the Swift side but Realm Database is just as easy to use with other programming languages.

You can easily peak at the docs online here:

This post is just the beginning in our series on Realm Database and we hope you enjoyed it. In the next installment we’re going to look into some of the RealmSwift API classes that allow for creating easily performant and optimized apps.

See you next time! 👋

Next Up: What makes Realm different #2: The Realm API is Optimized for Performance and Low Memory Use

General link arrow white

About the content

This content has been published here with the express permission of the author.

Marin Todorov

Marin Todorov is an independent iOS consultant and publisher. He’s co-author on the book “RxSwift: Reactive programming with Swift” the author of “iOS Animations by Tutorials”. He’s part of Realm and Besides crafting code, Marin also enjoys blogging, writing books, teaching, and speaking. He sometimes open sources his code. He walked the way to Santiago.

4 design patterns for a RESTless mobile integration »