Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Configure and Open a Realm - SwiftUI

On this page

  • Open a Realm with a Configuration
  • Open a Synced Realm
  • Download Changes Before Opening a Synced Realm
  • Open a Synced Realm Offline

The Swift SDK provides property wrappers to open a realm in a SwiftUI-friendly way.

You can:

  • Implicitly open a realm with a defaultConfiguration or specify a different configuration. This works for both non-synced and synced realms.

  • Always download changes before opening a synced realm, which times out when the user is offline.

  • Open a synced realm even when a user is offline. The realm may lack the most recent data.

When you use @ObservedRealmObject or @ObservedResults, these property wrappers implicitly open a realm and retrieve the specified objects or results.

// Implicitly use the default realm's objects(Dog.self)
@ObservedResults(Dog.self) var dogs

Note

The @ObservedResults property wrapper is intended for use in a SwiftUI View. If you want to observe results in a view model, register a change listener.

When you do not specify a configuration, these property wrappers use the defaultConfiguration. You can set the defaultConfiguration globally, and property wrappers across the app can use that configuration when they implicitly open a realm.

You can provide alternative configurations that the property wrappers use to implicitly open the realm. You might want to do this when using multiple configurations in your app, as in cases where you have both a SyncConfiguration and a local Configuration. To do this, create explicit configurations. Then, use environment injection to pass the respective configurations to the views that need them. Passing a configuration to a view where property wrappers open a realm uses the passed configuration instead of the defaultConfiguration.

New in version 10.12.0.

These SwiftUI property wrappers open synced realms and populate views. The main difference between these property wrappers is whether the user must be online:

  • To download updates from your Atlas App Services app before opening a realm, use the @AsyncOpen property wrapper. This requires the user to have a network connection.

  • To open a synced realm regardless of whether the user has a network connection, use the @AutoOpen property wrapper. This property wrapper enables developers to design offline-first capabilities into their apps.

Tip

Migrate to Flexible Sync

You can automatically migrate your App Services Device Sync Mode from Partition-Based Sync to Flexible Sync. This enables you to take advantage of the more expressive and granular Flexible Sync subscriptions and permissions to manage what synced data your users can read and write. For more information, refer to Migrate from Partition-Based Sync to Flexible Sync.

Use the @AsyncOpen property wrapper for apps that require up-to-date information from the server, such as game apps with live leaderboards that the user can play on multiple devices. This ensures the user is never using the app with stale data.

This SwiftUI property wrapper initiates Realm.asyncOpen() for the current user. The property wrapper publishes states, represented by the AsyncOpenState enum, which you can use to update the view.

Example

This example illustrates one way you might use @AsyncOpen to open a realm in a view. First, check for a user, or log them in. Then, attempt to open the realm, switching on the AsyncOpenState to display an appropriate view. When the realm opens successfully, inject it as an environment value to populate the view.

Like @AsyncOpen, @AutoOpen attempts to download updates before opening the realm. However, if a network connection is not available, this method instead opens a realm with data on the device.

Use this property wrapper for apps where it's not a problem for the user to work with potentially stale data, such as note-taking apps where users should be able to work with data on the device

This SwiftUI property wrapper attempts to download updates before opening a realm for the current user. If there is no internet connection, this property wrapper instead returns the most up-to-date version of the local realm file for the given appId and Flexible Sync or Partition-Based Sync configuration.

The property wrapper publishes states, represented by the AsyncOpenState enum, which you can use to update the view. For a full example, see the @AsyncOpen code examples above.

← 
 →