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.
@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.
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.
New in version 10.27.0.
Realm Swift SDK version 10.27.0 adds Flexible Sync versions of the property wrappers to open Realm with SwiftUI. You can add subscription queries in .onAppear
after opening the realm.
@AsyncOpen(appId: flexibleSyncAppId, timeout: 4000) var asyncOpen
New in version 10.28.0.
You can create a flexibleSyncConfiguration() with the initialSubscriptions
parameter. You can use this parameter to subscribe to Flexible Sync queries in the configuration. If this runs more than once - for example, if it's in a view that reloads regularly - check whether the subscription exists already before adding it. Adding the same subscription again throws an error.
let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in let peopleSubscriptionExists = subs.first(named: "people") let dogSubscriptionExists = subs.first(named: "dogs") if (peopleSubscriptionExists != nil) && (dogSubscriptionExists != nil) { return } else { subs.append(QuerySubscription<Person>(name: "people")) subs.append(QuerySubscription<Dog>(name: "dogs")) }})
Then, pass the configuration to the view that contains the property wrappers as an environment object.
OpenFlexibleSyncRealmView() .environment(\.realmConfiguration, config)
For a complete example, see the SwiftUI Quick Start.
To open a realm with Partition-Based Sync, add a partitionValue
to the property wrapper:
@AsyncOpen(appId: YOUR_APP_SERVICES_APP_ID_HERE, partitionValue: "", timeout: 4000) var asyncOpen
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.
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.
struct OpenFlexibleSyncRealmView: View { @AsyncOpen(appId: flexibleSyncAppId, timeout: 4000) var asyncOpen var body: some View { switch asyncOpen { case .connecting: ProgressView() case .waitingForUser: ProgressView("Waiting for user to log in...") case .open(let realm): UseRealmView(realm: realm) case .progress(let progress): ProgressView(progress) case .error(let error): ErrorView(error: error) } }}
struct OpenPartitionBasedSyncRealm: View { @AsyncOpen(appId: YOUR_APP_SERVICES_APP_ID_HERE, partitionValue: "", timeout: 4000) var asyncOpen var body: some View { switch asyncOpen { case .connecting: ProgressView() case .waitingForUser: ProgressView("Waiting for user to log in...") case .open(let realm): UseRealmView(realm: realm) case .progress(let progress): ProgressView(progress) case .error(let error): ErrorView(error: error) } }}
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
@AutoOpen(appId: "app_id") var autoOpen
@AutoOpen(appId: "app_id", partitionValue: <partition_value>) var autoOpen
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4