Docs Menu

Realm Swift SDK

On this page

The Realm Database Swift SDK enables mobile applications to access data stored in local realms. Optionally, interact with Atlas App Services features such as Functions, MongoDB Data Access, and authentication. The Realm Swift SDK supports Swift and Objective-C, and provides SwiftUI-friendly property wrappers.

See also:

Are you looking for information about using Swift with MongoDB in a backend app, command-line utility, or running on macOS or Linux? See the MongoDB Swift Driver documentation.

With the Realm Swift SDK, you can access objects stored in a local instance of Realm Database.

Use Swift to idiomatically define an object schema.

class CoffeeDrink: Object {
@Persisted var name = ""
@Persisted var hotOrCold: String?
@Persisted var rating = 0

Create a new object as you would instantiate any other object. Then, pass it to Realm Database inside a write transaction.

let realm = try! Realm()
try! realm.write {
// Add coffee shop and drink info here.
let shop = CoffeeShop() = "Better Coffee"
let drink = CoffeeDrink() = "Maple Latte"
drink.rating = 7

Query for stored objects using Swift-idiomatic queries.

let realm = try! Realm()
let drinks = realm.objects(CoffeeDrink.self)
let highlyRatedDrinks = drinks.where {
$0.rating > 6
print("Highly-rated drinks: \(highlyRatedDrinks.count)")
let mapleOrCaramelLattes = drinks.where {
$ == "Maple Latte" || $ == "Caramel Latte"
print("Number of maple or caramel lattes: \(mapleOrCaramelLattes.count)")
let drinkTempNotSpecified = drinks.where {
$0.hotOrCold == nil
print("No info about drink temp: \(drinkTempNotSpecified.count)")

Update objects by updating field values on an instance of the object within a transaction.

let realm = try! Realm()
// Get a maple latte
let mapleLatte = realm.objects(CoffeeDrink.self).where {
$ == "Maple Latte"
// Open a thread-safe transaction
try! realm.write {
// Change the name of the maple latte. = "Maple Delight"
// Specify that the maple latte is a hot drink.
mapleLatte.hotOrCold = "Hot"
} // When the transaction completes, the drink details are updated in the database.

Register a notification handler on an entire realm, a collection, or an object, and react to changes.

var drinkNotificationToken: NotificationToken?
func objectNotificationExample() {
// Create a new drink
let drink = CoffeeDrink() = "Spicy Icy Coffee"
drink.rating = 9
// Open the default realm.
let realm = try! Realm()
try! realm.write {
// Add the drink to the realm
// Observe changes.
drinkNotificationToken = drink.observe { change in
switch change {
case .change(let object, let properties):
for property in properties {
print("Property '\(' of object \(object) changed to '\(property.newValue!)'")
case .error(let error):
print("An error occurred: \(error)")
case .deleted:
print("The object was deleted.")
// Now, when you update the object, this triggers the notification
try! realm.write { = "Ancho Chili Chocolate Iced Coffee"

Because Realm Database objects are live objects, they're automatically updated when they're modified.

// Open the default realm.
let realm = try! Realm()
// Create a couple of references to a single underlying coffee drink object
let drinkA = realm.objects(CoffeeDrink.self).where {
$ == "Maple Latte"
let drinkB = realm.objects(CoffeeDrink.self).where {
$ == "Maple Latte"
// Update drink A's name
try! realm.write { = "Maple-iest Latte in Town"
// See that drink B instance updates with the new name
XCTAssert( ==
// Update drink B's rating
try! realm.write {
drinkB.rating = 4
// See that drink A instance updates with the new rating
XCTAssert(drinkB.rating == drinkA.rating)

Atlas App Services Apps, which we may refer to as Apps, are backends for client applications hosted by MongoDB in the cloud. They provide the ability to synchronize data stored in Realm Database, called Atlas Device Sync, as well as a layer of backend functionality collectively called App Services. In addition to working with local Realm Database, the Swift SDK also enables you to leverage the features of Apps.

Use Atlas Device Sync to automatically sync your realms across client devices and a MongoDB Atlas data store backend.


Atlas Device Sync does not currently support watchOS.

When you create an App, you get access to a variety of services to streamline app development:

  • User management: built-in user management to enable account creation and authentication
  • Functions: define and execute server-side logic
  • Triggers: react to events or define a schedule to execute automated processes
Get Started
Realm Apps Examples

For information about advanced concepts, such as encryption and threading, see the Advanced Guides section.

Supported OS
Realm Database
Realm Apps
iOS 9.0+ (iOS 11+ if using Swift Package Manager)
macOS 10.9+ (macOS 10.10+ if using Swift Package Manager)
tvOS 9.0+
watchOS 2.0+

There are special considerations when using Realm Database with tvOS. See Build for tvOS for more information.

Starting with Realm Swift SDK Versions 10.15.0 and 10.16.0, many of the Realm APIs support the Swift async/await syntax. Projects must meet these requirements:

Swift SDK Version
Swift Version Requirement
Supported OS
Swift 5.6
iOS 13.x
10.15.0 or 10.16.0
Swift 5.5
iOS 15.x
←  Upgrade from Stitch to Realm - React Native SDKInstall Realm for iOS, macOS, tvOS, and watchOS →
Give Feedback
© 2022 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.