Realm Database with SwiftUI QuickStart
On this page
Prerequisites
- Have Xcode 12.4 or later (minimum Swift version 5.3.1).
- Create a new Xcode project using the SwiftUI "App" template with a minimum iOS target of 15.0.
- Install the Swift SDK. This SwiftUI app requires a minimum SDK version of 10.19.0.
Overview
This page provides a small working app to get you up and running with Realm and SwiftUI quickly. If you'd like to see additional examples, including more explanation about Realm's SwiftUI features, see: Use Realm Database with SwiftUI.
This page contains all of the code for a working Realm and SwiftUI app. The app starts on the ItemsView, where you can edit a list of items:
- Press the Add button on the bottom right of the screen to add randomly-generated items.
- Press the Edit button on the top right to modify the list order, which the app persists in the realm.
- You can also swipe to delete items.
When you have items in the list, you can press one of the items to navigate to the ItemDetailsView. This is where you can modify the item name or mark it as a favorite:
- Press the text field in the center of the screen and type a new name. When you press Return, the item name should update across the app.
- You can also toggle its favorite status by pressing the heart toggle in the top right.
This guide optionally integrates with Atlas Device Sync. See Integrate Atlas Device Sync below.
Get Started
We assume you have created an Xcode project with the SwiftUI "App"
template. Open the main Swift file and delete all of the code inside,
including any @main
App
classes that Xcode generated for you. At
the top of the file, import the Realm and SwiftUI frameworks:
import RealmSwift import SwiftUI
Just want to dive right in with the complete code? Jump to Complete Code below.
Define Models
A common Realm data modeling use case is to have "things" and "containers of things". This app defines two related Realm object models: item and itemGroup.
An item has two user-facing properties:
- A randomly generated-name, which the user can edit.
- An
isFavorite
boolean property, which shows whether the user "favorited" the item.
An itemGroup contains items. You can extend the itemGroup to have a name and an association with a specific user, but that's out of scope of this guide.
Paste the following code into your main Swift file to define the models:
Views and Observed Objects
The entrypoint of the app is the ContentView class that derives from
SwiftUI.App
. For now, this always displays the
LocalOnlyContentView. Later, this will show the SyncContentView
when Atlas Device Sync is enabled.
/// The main screen that determines whether to present the SyncContentView or the LocalOnlyContentView. /// For now, it always displays the LocalOnlyContentView. @main struct ContentView: SwiftUI.App { var body: some Scene { WindowGroup { LocalOnlyContentView() } } }
You can use a realm other than the default realm by passing an environment object from higher in the View hierarchy:
LocalOnlyContentView() .environment(\.realmConfiguration, Realm.Configuration( /* ... */ ))
The LocalOnlyContentView has an @ObservedResults itemGroups. This implicitly uses the default realm to load all itemGroups when the view appears.
This app only expects there to ever be one itemGroup. If there is an itemGroup in the realm, the LocalOnlyContentView renders an ItemsView for that itemGroup.
If there is no itemGroup already in the realm, then the
LocalOnlyContentView displays a ProgressView while it adds one. Because
the view observes the itemGroups thanks to the @ObservedResults
property
wrapper, the view immediately refreshes upon adding that first itemGroup and
displays the ItemsView.
/// The main content view if not using Sync. struct LocalOnlyContentView: View { // Implicitly use the default realm's objects(ItemGroup.self) ItemGroup.self) var itemGroups ( var body: some View { if let itemGroup = itemGroups.first { // Pass the ItemGroup objects to a view further // down the hierarchy ItemsView(itemGroup: itemGroup) } else { // For this small app, we only want one itemGroup in the realm. // You can expand this app to support multiple itemGroups. // For now, if there is no itemGroup, add one here. ProgressView().onAppear { $itemGroups.append(ItemGroup()) } } } }
Starting in SDK version 10.12.0, you can use an optional key path parameter
with @ObservedResults
to filter change notifications to only those
occurring on the provided key path or key paths. For example:
@ObservedResults(MyObject.self, keyPaths: ["myList.property"])
The ItemsView receives the itemGroup from the parent view and stores it in an @ObservedRealmObject property. This allows the ItemsView to "know" when the object has changed regardless of where that change happened.
The ItemsView iterates over the itemGroup's items and passes each item to an ItemRow for rendering as a list.
To define what happens when a user deletes or moves a row, we pass the
remove
and move
methods of the Realm
List as the handlers of the respective
remove and move events of the SwiftUI List. Thanks to the
@ObservedRealmObject
property wrapper, we can use these methods
without explicitly opening a write transaction. The property wrapper
automatically opens a write transaction as needed.
/// The screen containing a list of items in a itemGroup. Implements functionality for adding, rearranging, /// and deleting items in the itemGroup. struct ItemsView: View { /// The itemGroup is a container for a list of items. Using an itemGroup instead of all items /// directly allows us to maintain a list order that can be updated in the UI. var itemGroup: ItemGroup /// The button to be displayed on the top left. var leadingBarButton: AnyView? var body: some View { NavigationView { VStack { // The list shows the items in the realm. List { ForEach(itemGroup.items) { item in ItemRow(item: item) }.onDelete(perform: $itemGroup.items.remove) .onMove(perform: $itemGroup.items.move) }.listStyle(GroupedListStyle()) .navigationBarTitle("Items", displayMode: .large) .navigationBarBackButtonHidden(true) .navigationBarItems( leading: self.leadingBarButton, // Edit button on the right to enable rearranging items trailing: EditButton()) // Action bar at bottom contains Add button. HStack { Spacer() Button(action: { // The bound collection automatically // handles write transactions, so we can // append directly to it. $itemGroup.items.append(Item()) }) { Image(systemName: "plus") } }.padding() } } } }
Finally, the ItemRow and ItemDetailsView classes use the
@ObservedRealmObject
property wrapper with the item passed in from
above. These classes demonstrate a few more examples of how to use the
property wrapper to display and update properties.
/// Represents an Item in a list. struct ItemRow: View { var item: Item var body: some View { // You can click an item in the list to navigate to an edit details screen. NavigationLink(destination: ItemDetailsView(item: item)) { Text(item.name) if item.isFavorite { // If the user "favorited" the item, display a heart icon Image(systemName: "heart.fill") } } } } /// Represents a screen where you can edit the item's name. struct ItemDetailsView: View { var item: Item var body: some View { VStack(alignment: .leading) { Text("Enter a new name:") // Accept a new name TextField("New name", text: $item.name) .navigationBarTitle(item.name) .navigationBarItems(trailing: Toggle(isOn: $item.isFavorite) { Image(systemName: item.isFavorite ? "heart.fill" : "heart") }) }.padding() } }
@ObservedRealmObject
is a frozen object. If you want to modify
the properties of an @ObservedRealmObject
directly in a write transaction, you must .thaw()
it first.
At this point, you have everything you need to work with Realm Database and SwiftUI. Test it out and see if everything is working as expected. Read on to learn how to integrate this app with Atlas Device Sync.
Integrate Atlas Device Sync
Now that we have a working Realm Database app, we can optionally integrate with Atlas Device Sync. Sync allows you to you see the changes you make across devices. Before you can add sync to this app, make sure to:
- Create an Atlas App Services App.
- Enable anonymous authentication.
- Choose Partition-Based Sync or Flexible Sync
- Specify a cluster and database.
- Turn on Development Mode.
- For Partition-Based Sync, use
_partition
as a partition key. If you're using Flexible Sync, useownerId
as the queryable field. For Partition-Based Sync Permissions, select the template: User can only read and write their own data. For Flexible Sync, use these permissions:
{ "rules": {}, "defaultRoles": [ { "name": "owner-read-write", "applyWhen": {}, "read": { "ownerId": "%%user.id" }, "write": { "ownerId": "%%user.id" } } ] } - Enable Sync, and deploy your application updates.
The Sync version of this app changes the app flow a bit. The first screen becomes the LoginView. When you press the Log in button, the app navigates to the ItemsView, where you see the synced list of items in a single itemGroup.
At the top of the source file, initialize an optional Realm app with your Atlas App Services App ID:
// MARK: Atlas App Services (Optional) // The Atlas App Services app. Change YOUR_APP_SERVICES_APP_ID_HERE to your App Services app ID. // If you don't have an App Services app and don't wish to use Sync for now, // you can change this to: // let app: RealmSwift.App? = nil let app: RealmSwift.App? = RealmSwift.App(id: YOUR_APP_SERVICES_APP_ID_HERE)
You can change the app reference to nil
to switch back to
local-only (non-Atlas Device Sync) mode.
Let's update the main ContentView to show the SyncContentView if the
app reference is not nil
:
/// The main screen that determines whether to present the SyncContentView or the LocalOnlyContentView. @main struct ContentView: SwiftUI.App { var body: some Scene { WindowGroup { // Using Sync? if let app = app { SyncContentView(app: app) } else { LocalOnlyContentView() } } } }
We define the SyncContentView below.
The SyncContentView observes the Realm app instance. The app instance is the interface to the Atlas App Services backend, which provides the user authentication required for Sync. By observing the app instance, the SyncContentView can react when a user logs in or out.
This view has two possible states:
- If the Realm app does not have a currently logged-in user, show the LoginView.
- If the app does have a logged-in user, show the OpenSyncedRealmView.
Here's the code for the SyncContentView:
Once logged in, we open the realm asynchronously with the AsyncOpen property wrapper.
The OpenSyncedRealmView switches on the AsyncOpenState
enum, which lets us show different views
based on the state. In our example, we show a ProgressView
while we're
connecting to the App and the realm is syncing. We then open the
realm, passing the itemGroup
to the ItemsView
, or show an
ErrorView
if we can't open the realm.
When opening a synced realm, use the AsyncOpen property wrapper to always download synced changes
before opening the realm, or the AutoOpen property wrapper to open a realm while syncing
in the background. AsyncOpen
requires the user to be online,
while AutoOpen
opens a realm even if the user is offline.
This view has a few different states:
- While connecting or waiting for login, show a
ProgressView
. - While downloading changes to the realm, show a
ProgressView
with a progress indicator. - When the realm opens, check for an itemGroup object. If one does not exist yet, create one. Then, show the ItemsView for the itemGroup in the realm. Provide a LogoutButton that the ItemsView can display on the top left of the navigation bar.
- If there is an error loading the realm, show an error view containing the error.
When you run the app and see the main UI, there are no items in the view. That's because we're using anonymous login, so this is the first time this specific user logs in.
Authenticate Users with Atlas App Services
The LoginView maintains some state in order to display an activity indicator or error. It uses a reference to the Realm app instance passed in from above to log in when the Log in anonymously button is clicked.
In the LoginView, you can implement email/password authentication or another authentication provider. For simplicity, this example uses Anonymous authentication.
Once login is complete, the LoginView itself doesn't need to do anything more. Because the parent view is observing the Realm app, it will notice when the user authentication state has changed and decide to show something other than the LoginView.
/// Represents the login screen. We will have a button to log in anonymously. struct LoginView: View { // Hold an error if one occurs so we can display it. var error: Error? // Keep track of whether login is in progress. var isLoggingIn = false var body: some View { VStack { if isLoggingIn { ProgressView() } if let error = error { Text("Error: \(error.localizedDescription)") } Button("Log in anonymously") { // Button pressed, so log in isLoggingIn = true app!.login(credentials: .anonymous) { result in isLoggingIn = false if case let .failure(error) = result { print("Failed to log in: \(error.localizedDescription)") // Set error to observed property so it can be displayed self.error = error return } // Other views are observing the app and will detect // that the currentUser has changed. Nothing more to do here. print("Logged in") } }.disabled(isLoggingIn) } } }
The LogoutButton works just like the LoginView, but logs out instead of logging in:
/// A button that handles logout requests. struct LogoutButton: View { var isLoggingOut = false var body: some View { Button("Log Out") { guard let user = app!.currentUser else { return } isLoggingOut = true user.logOut() { error in isLoggingOut = false // Other views are observing the app and will detect // that the currentUser has changed. Nothing more to do here. print("Logged out") } }.disabled(app!.currentUser == nil || isLoggingOut) } }
Once logged in, the app follows the same flow as the local-only version.
Complete Code
In case you would like to copy and paste or examine the complete code with or without Atlas Device Sync, see below.