Open Synced Realms in SwiftUI using @Auto/AsyncOpen
Rate this announcement
We’re very happy to announce that v10.12.0 of the includes our two new property wrappers and for asynchronous opening of a realm for users. This new feature, which is a response to your community feedback, aligns with our goal to make our developer experience better and more effortless, integrating it with SwiftUI, and removing boilerplate code.
Up until now, the standard approach for opening a realm for any sync user is to call
Realm.asyncOpen()using a user’s sync configuration, then publish the opened Realm to the view:
@AutoOpen, we are reducing development time and boilerplate, making it easier, faster, and cleaner to implement Realm.asyncOpen().
@AutoOpengive the user the possibility to cover two common use cases in synced apps.
@AsyncOpenproperty wrapper, we have the same behavior as using
Realm.asyncOpen(), but with a much more natural API for SwiftUI developers. Using this property wrapper prevents your app from trying to fetch the Realm file if there is no network connection, and it will only return a realm when it's synced with MongoDB Realm data. If there is no internet connection, then @AsyncOpen< will throw an error.
Let’s take, for example, a game app, which the user can play both on an iPhone and iPad. Having the data not updated would result in losing track of the current status of the player. In this case, it’s very important to have our data updated with any latest changes. This is the perfect use case for
This property wrapper's API gives you the flexibility to optionally specify a MongoDB Realm AppId. If no AppId is provided, and you’ve only used one ID within your App, then that will be used. You can also provide a timeout for your asynchronous operation:
Adding it to your SwiftUI App is as simple as declaring it in your view and have your view react to the state of the sync operation:
- Display a progress view while downloading or waiting for a user to be logged in.
- Display an error view if there is a failure during sync.
- Navigate to a new view after our realm is opened
Once the synced realm has been successfully opened, you can pass it to another view (embedded or via a navigation link):
If you have been using Realm.asyncOpen() in your current SwiftUI App and want to maintain the same behavior, you may want to migrate to
@AsyncOpen. It will simplify your code and make it more intuitive.
@AutoOpenshould be used when you want to work with the synced realm file even when there is no internet connection.
Let’s take, for example, Apple’s Notes app, which tries to sync your data if there is internet access and shows you all the notes synced from other devices. If there is no internet connection, then Notes shows you your local (possibly stale) data. This use case is perfect for the
@AutoOpenproperty wrapper. When the user recovers a network connection, Realm will sync changes in the background, without the need to add any extra code.
The syntax for using
@AutoOpenis the same as for
We added a new key to our set of Environment Values: a “partition value” environment key which is used by our new property wrappers
@AutoOpento dynamically inject a partition value when it's derived and not static. For example, in the case of using the user id as a partition value, you can pass this environment value to the view where
With these property wrappers, we continue to better integrate Realm into your SwiftUI apps. With the release of this feature, and more to come, we want to make it easier for you to incorporate our SDK and sync functionality into your apps, no matter whether you’re using UIKit or SwiftUI.