Quick Start


This page contains information to quickly get Realm Database integrated into your app. Before you begin, ensure you have:

Check Out the Tutorial

This page contains only the essential information that you need to set up a MongoDB Realm application. If you prefer to follow a guided tutorial that shows you step-by-step how to set up a working app, check out the iOS Tutorial where you’ll build a mobile app that connects to the Task Tracker backend.

Import Realm

Near the top of any Swift file that uses Realm, add the following import statement:

import RealmSwift

Initialize the App

To use MongoDB Realm features such as authentication and sync, you must access your Realm app using your Realm app ID. You can find your Realm app ID in the Realm UI.

let app = RealmApp(id: "your-realm-app-id")

Define Your Object Model

If have not enabled Realm Sync or you enabled Sync with Development Mode in the Realm UI, you can define your object model directly in code.


If you have enabled Sync but turned off Developer Mode, you can copy and paste the object model definitions that Realm generated for you from the SDKs tab in the Realm UI. You must re-enable Developer Mode if you want to make changes to the object model definition from client side code. See Configure Your Data Model.

class Task: Object {
    @objc dynamic var _id: ObjectId = ObjectId.generate()

    // When configuring Sync, we selected `_partition` as the partition key.
    // A partition key is only required if you are using Sync.
    @objc dynamic var _partition: String = ""

    @objc dynamic var name: String? = nil
    @objc dynamic var complete = false

    override static func primaryKey() -> String? {
        return "_id"

    convenience init(partition: String, name: String) {
        self._partition = partition = name

Authenticate a User

When you have enabled anonymous authentication in the Realm UI, users can immediately log into your app without providing any identifying information:

app.login(withCredential: AppCredentials.anonymous()) { (user, error) in
     // Remember to dispatch back to the main thread in completion handlers
     // if you want to do anything on the UI.
     DispatchQueue.main.sync {
       guard error == nil else {
         print("Login failed: \(error!)")

       // Now logged in, do something with user


Realm provides many additional ways to authenticate, register, and link users.

Open a Realm

Once you have enabled Realm Sync and authenticated a user, you can open a synced realm:

let user = app.currentUser()
let partitionValue = "myPartition"
realm = try! Realm(configuration: user.configuration(partitionValue: partitionValue))

See also

Sync Data

Create, Read, Update, and Delete Objects

Once you have opened a realm, you can modify it and its objects in a write transaction block.

To create a new Task, instantiate the Task class and add it to the realm in a write block:

let task = Task(partition: partitionValue, name: "New Task")
try! self.realm.write {

You can retrieve a live collection of all tasks in the realm:

// All tasks in the realm
let tasks = realm.objects(Task.self)

You can also filter that collection using a filter:

// You can also filter a collection
let tasksThatBeginWithA = tasks.filter("name beginsWith 'A'")
let completeTasks = tasks.filter("complete = true")

To modify a task, simply update its properties in a write transaction block:

let task = tasks[someIndex]

// All modifications to a realm must happen in a write block.
try! realm.write {
    task.completed = true

Finally, you can delete a task:

let task = tasks[someIndex]

// All modifications to a realm must happen in a write block.
try! realm.write {
    // Delete the Task.

Watch for Changes

You can watch a realm, collection, or object for changes with the observe method.


Be sure to retain the notification token returned by observe as long as you want to continue observing. When you are done observing, invalidate the token to free the resources.

// Retain notificationToken as long as you want to observe
notificationToken = tasks.observe { (changes) in
     switch changes {
     case .initial:
         // Results are now populated and can be accessed without blocking the UI
     case .update(_, let deletions, let insertions, let modifications):
         // Query results have changed.
         print("Deleted indices: ", deletions)
         print("Inserted indices: ", insertions)
         print("Modified modifications: ", modifications)
     case .error(let error):
         // An error occurred while opening the Realm file on the background worker thread

// Later, when done observing

Log Out

Once logged in, you can log out:

app.logOut(completion: { (error) in
    // Logged out or error occurred