BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Realm
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

Open Synced Realms in SwiftUI using @Auto/AsyncOpen

Diana Perez Afanador4 min read • Published Aug 25, 2021 • Updated Oct 19, 2022
Realm
Facebook Icontwitter iconlinkedin icon
Rate this announcement
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

We’re very happy to announce that v10.12.0 of the Realm Cocoa SDK includes our two new property wrappers @AutoOpen and @AsyncOpen for asynchronous opening of a realm for Realm Sync 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:
With @AsyncOpen and @AutoOpen, we are reducing development time and boilerplate, making it easier, faster, and cleaner to implement Realm.asyncOpen(). @AsyncOpen and @AutoOpen give the user the possibility to cover two common use cases in synced apps.
Build better mobile apps with Atlas Device Sync: Atlas Device Sync is a fully-managed mobile backend-as-a-service. Leverage out-of-the-box infrastructure, data synchronization capabilities, built-in network handling, and much more to quickly launch enterprise-grade mobile apps. Get started now by build: Deploy Sample for Free!

Prerequisites

@AsyncOpen

With the @AsyncOpen property 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 @AsyncOpen.
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.

@AutoOpen

@AutoOpen should 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 @AutoOpen property 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 @AutoOpen is the same as for @AsyncOpen:

One Last Thing…

We added a new key to our set of Environment Values: a “partition value” environment key which is used by our new property wrappers @AsyncOpen and @AutoOpen to 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 @AsyncOpen or @AutoOpen are used:

Conclusion

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.
We are excited for our users to test these new features. Please share any feedback or ideas for new features in our community forum.
Documentation on both of these property wrappers can be found in our docs.

Facebook Icontwitter iconlinkedin icon
Rate this announcement
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

How to Do Full-Text Search in a Mobile App with MongoDB Realm


Jul 14, 2023 | 3 min read
Tutorial

Online/Offline Data-Capable Cross-Platform Apps with MongoDB Atlas, Atlas Device SDKs and .NET MAUI


Nov 14, 2023 | 6 min read
Code Example

Build Offline-First Mobile Apps by Caching API Results in Realm


Mar 06, 2023 | 11 min read
Tutorial

Using Maps and Location Data in Your SwiftUI (+Realm) App


Aug 26, 2022 | 8 min read
Table of Contents
  • Introduction