Docs Menu

MongoDB Realm iOS SDK

On this page

  • Realm Database
  • Define an Object Schema
  • Write an Object
  • Query Realm Database
  • Update Live Objects
  • Watch for Updates
  • Always Access the Latest Data
  • Realm Apps
  • Realm Sync
  • App Services
  • Get Started
  • Advanced Guides
  • Realm Database
  • Realm Apps
  • Integration Guides
  • OS Support

The MongoDB Realm Swift SDK enables mobile applications to access data stored in local realms. Optionally, interact with MongoDB Realm services like Functions, MongoDB Data Access, and authentication.

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 MongoDB 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 directly from your client application code.

let realm = try! Realm()
let drinks = realm.objects(CoffeeDrink.self)
let highlyRatedDrinks = drinks.filter("rating > 6")
print("Highly-rated drinks: \(highlyRatedDrinks.count)")
let mapleOrCaramelLattes = drinks.filter("name IN {'Maple Latte', 'Caramel Latte'}")
print("Number of maple or caramel lattes: \(mapleOrCaramelLattes.count)")
let drinkTempNotSpecified = drinks.filter("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).filter("name == 'Maple Latte'").first!
// 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).filter("name == 'Maple Latte'").first!
let drinkB = realm.objects(CoffeeDrink.self).filter("name == 'Maple Latte'").first!
// 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)

Realm apps are backends for client applications hosted by MongoDB in the cloud. They provide the ability to synchronize data stored in Realm Database, called Realm 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 Realm apps.

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


Realm Sync does not currently support watchOS.

When you create a Realm 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.

Give Feedback
© 2021 MongoDB, Inc.


  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.