Navigation

MongoDB Realm iOS SDK

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.

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 {
@objc dynamic var name = ""
@objc dynamic var hotOrCold: String?
@objc dynamic 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()
shop.name = "Better Coffee"
let drink = CoffeeDrink()
drink.name = "Maple Latte"
drink.rating = 7
shop.drinks.append(drink)
realm.add(shop)
}

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.
mapleLatte.name = "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()
drink.name = "Spicy Icy Coffee"
drink.rating = 9
// Open the default realm.
let realm = try! Realm()
try! realm.write {
// Add the drink to the realm
realm.add(drink)
}
// Observe changes.
drinkNotificationToken = drink.observe { change in
switch change {
case .change(let object, let properties):
for property in properties {
print("Property '\(property.name)' 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 {
drink.name = "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 {
drinkA.name = "Maple-iest Latte in Town"
}
// See that drink B instance updates with the new name
XCTAssert(drinkA.name == drinkB.name)
// 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.

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
Fundamentals
Examples
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 8.0+
X
X
macOS 10.9+
X
X
tvOS 9.0+
X
X
watchOS 2.0+
X
Important

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

Give Feedback