Skip to content
tezvyn:
Menu

Realm Database

Source: mongodb.comMediumHow cards are made

Realm Database

Realm is an object oriented mobile database that stores your Swift model objects directly, without an ORM layer translating to and from SQL, and gives you live objects that automatically update in your UI whenever the underlying data changes.

Why it exists

Core Data solves mobile persistence but comes with a steep learning curve around its object graph, contexts, merge policies and thread confinement rules, and raw SQLite means writing and maintaining SQL by hand plus a manual mapping layer to your model types. Realm exists to offer object persistence that feels like working with plain Swift objects, backed by a purpose built mobile database engine, without needing to hand write SQL or manage Core Data's context machinery.

The mental model

Think of a normal database as a filing cabinet: you ask a clerk to fetch a folder, get a photocopy, and if the original changes later your photocopy is now stale. A Realm object is more like a live window into the actual folder in the cabinet: what you are looking at updates itself the instant something else changes that same folder, because you are not holding a copy, you are holding a reference straight into Realm's underlying storage.

How it works

You define a model by subclassing Object and declaring its properties, then write with realm.write inside a closure, which wraps your changes in an ACID transaction and commits them atomically. Fetching with realm.objects(Type.self) returns a Results collection that is lazily loaded and stays live, meaning as the underlying data changes, previously fetched objects and result collections update automatically and can push out change notifications you subscribe to. Under the hood, Realm uses a zero copy architecture, memory mapping database pages directly rather than deserializing full objects into memory, and multiversion concurrency control lets many threads read a consistent snapshot while a write happens elsewhere, though a Realm instance stays confined to the thread that created it.

When it matters

Realm matters when a mobile app needs fast local persistence with a lot of reactive UI, like a chat or feed app where new local data should appear instantly without manual refresh logic. The footgun is thread confinement: passing a Realm object across threads directly crashes or corrupts state, so you pass primary keys or thread safe references between threads and refetch the object on the receiving thread's own Realm instance.

A concrete example

A note taking app fetches a Results collection of all Note objects sorted by date and binds it to a SwiftUI list. When a background sync task inserts a new note fetched from the server inside a write transaction, the already displayed Results collection updates itself, and the SwiftUI list re-renders with the new note in place, with no explicit refetch call anywhere in the view code.

Interview question

A developer fetches a Realm Results collection of Task objects on the main thread, then passes that same live collection directly into a closure that runs on a background thread to read from it. What is the problem with this?

  • a.Nothing is wrong, Realm objects and Results are automatically thread safe like most Swift collections
  • b.The problem is only that the background thread's copy of the data will be permanently stale
  • c.Realm objects and Results are confined to the thread that created them, so accessing them from another thread directly is invalid, the fix is to open a new Realm on that thread and refetchCorrect
  • d.Results collections cannot be observed for changes, so this pattern is fine but simply loses live updates
Why?

Realm enforces thread confinement, live objects and Results are tied to the Realm instance and thread that created them, so crossing threads directly is unsafe, and the fix is to hand off a thread safe reference or primary key and refetch on the target thread. Option A is wrong because this is the opposite of Realm's actual threading contract, which is a common source of crashes for developers coming from thread safe database wrappers.

Just read this? Test yourself on what you have been reading.

Read the original → mongodb.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles