Realm

Enter into...

Realm

Enter into...

Realm is a mobile database that is sort of a replacement to SQLlite/Core Data that is known to be lightweight and even faster than it’s competition. Realm is free and relatively easy to include in your project (though many comment that it’s quite difficult to remove). One of the greatest advantages is that it flattens some of the traditional Data Access boilerplate and merges Models with Objects.

With Realm 2.0, a brand new Platform has been released complete with an object server. Whereas before a Realm was local to an app, it can now be synced to a server side database. This removes the dependency of translating and sending Realm data manually to some other server/cloud based database solution. It even handles the syncing automatically with very little code needed to setup.

Is Realm the best solution: it depends. But especially with the new platform, it makes it worth while to consider.

Resources to Get Started

Tech Details

Add to Project

Get all Realm items here

In the package you downloaded, find the SDK folder appropriate for your project. Take the framework files and add them to your project.

Another item in your project is the Xcode plugin. This plugin adds a Realm Object option when creating a New File.

Lastly, there is the Realm Browser app for macOS. This is a tool to explore the realm file created for your app. After running your app with realm the first time, locate the default.realm file in your app’s data and open with the Browser.

Tutorial

Simple Tutorial

Creating an object

Realm will be different for anyone who is used to creating a database table schema and then data object models to capture it, or vice-versa in previous projects. Since the database is local, there’s no need to do this work. Instead, we create the model in the code, and the table schema (as well as the table itself) is automatically created and maintained in a .realm file.

To do this, you just create a new class that extends the Realm Object. Here’s a quick sample:

class Person: Object { dynamic var name = "" dynamic var age = 0 }

Immediately we can start creating objects using our new class:

let danny = Person() danny.name = "Danny" danny.age = 27

We now have this class defined in our code and an instance of this class. However, we still do not have this stored in a Realm table, nor do we even have one defined. So, let’s start by creating a Realm (which is essentially a local database):

let realm = try! Realm()

This code sets up a Realm instance that is synced with our default realm file (automatically default.realm). (Note: you can specify your own realm files). In this code, because default.realm does not exist, it automatically creates the file.

Did you catch that? We just made a database by initializing a Realm. That’s how simple it is to get going. Now watch what it takes to create a table complete with a schema and an entry:

try! realm.write { realm.add(danny) }

That’s it. By simply setting a write block on the realm and adding our object, Realm has automatically taken care of setting up our table. How can we confirm?

let people = realm.objects(Person.self) print(people.count)

By calling the objects function on our realm, we request all entries that are of the type Person. When we print the count of the results, we should expect to see 1. If we wanted to filter for people whose age is 21 and over, we can add the filter to our query:

let people = realm.objects(Person.self).filter("age >= 21")

Using the filter function, we can add logic to get the results we want.

Lastly, if we want to simultaneously update an object as well as it’s corresponding entry, we can do the following:

let firstPerson = realm.objects(Person.self).first try! realm.write { firstPerson!.age = 28 }

Experiment

Can we create a common Realm Util?

Realm does allow for simple/plain code. For instance, models are extremely easy to setup and queries are very standard. Though you will quickly find blocks of code being repeated across different models. Also, there are no built-in converters, for example, JSON to Object. To be clear, there are, without a doubt, numerous edge cases. However, there are enough generic functions that, I feel, we can collect into a simple util class.