Realm + Codable. Part 1.

Denpazakura
3 min readDec 31, 2017

--

How Realm and JSON may simplify your work

A few months ago my colleague and I did a presentation for mobile developers at our company. The presentation was about Realm. My colleague did the theory part, and I showed how large amounts of JSON data can be parsed into a Realm database and immediately operated by the application.

It is hard to find a person who has been working in the mobile development for a while and hasn’t heard of it, though many projects favour the native Core Data or even plain SQLite.

Launched in 2014, Realm’s a cross-platform engine built with C++ at the core. The idea behind it was to avoid ORM and there’s no SQL under the hood. CRUD operations on Realm are much faster than those on the SQL database. This is reached thanks to zero-copy approach: objects are never copied to memory, instead you just work with the pointers.

Once I tried building an app with Realm, I’ve decided it’s the best database for mobile development so far, and here’s why.

Easy start

Realm is free and open-source. No need to be familiar with SQL statements or perform the NSManagedObjectContext dance. You operate Realm objects like any other objects, using your platform’s language.

Realm Objects are auto-updating

This allows your UI to be reactive without any additional efforts. However, subscribing to notifications is also available.

Local migrations are simple

The Core Data migration has often been a pain. Migrations in Realm just work, but make sure you know the current version of your schema.

You may update values in the migration block, rename properties or simply do nothing. The schema will be migrated to the provided version once the block is called.

Read more about migrations on realm.io.

Multithreading is (mostly) safe

WIth Realm you don’t need to worry about concurrency while working on the individual threads. Each thread has its own snapshot of the Realm; problems may rise if you need multiple threads share the same Realm instance. This is not possible as of now, so each thread must get its own instance.

Objects are treated as regular objects, but modifications should be wrapped into transactions:

The only thing you have to be aware of is that you cannot have multiple threads sharing the same instances of Realm objects. If multiple threads need to access the same objects they will each need to get their own instances (otherwise changes happening on one thread could cause other threads to see incomplete or inconsistent data).

Realm custom types

Object

Object is a class used to define Realm model objects. To make your model class work with Realm, you subclass Object, add properties and then use instances of your model classes as regular objects. Most of your model’s properties must be declared @objc dynamic var to become accessors for the underlying database data. Since Swift4, you may omit the @objc attribute if your class is marked @objcMembers.

LinkingObjects, List and RealmOptional

These properties are generic types and should be only declared as let.

LinkingObjects. This collection type is used to describe inverse relationships between objects. Linking objects are not stored into Realm by design, so making any use of them isn’t that simple (I’ll cover that in the second part of the article), and Realm does not support foreign keys.

List. List is a generic container type in Realm used to define to-many relationships. It is parameterized on the type of Object it stores and may be filtered with a predicate.

RealmOptional. This is a container type used for storing values that csn’t be declared as @objc in Swift, such as Int, Double, Float and Bool.

More about Realm collections

Pre-populating Realm database

If you are a mobile developer working on a big project and many people are involved, data collection should not be your problem. But even in this case you might be given a data source and asked to use it, not even to mention the situation when you have a small pet project and don’t have time to enter the data manually. Luckily, since Swift4 we can make use of the Decodable protocol. All you need is a JSON source, and most formats are convertible to JSON. If you’re lazy enough and are willing to automate boring stuff with Swift, Realm and Decodable, see Part 2.

--

--