EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
Realm
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Realmchevron-right

Using Realm Flexible Sync in Your App—an iOS Tutorial

Andrew Morgan12 min read • Published Feb 14, 2022 • Updated Oct 20, 2022
iOSRealmSync
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

In January 2022, we announced the release of the Realm Flexible Sync preview—an opportunity for developers to take it for a spin and give us feedback. Flexible Sync is now Generally Available as part of MongoDB Atlas Device Sync. That article provided an overview of the benefits of flexible sync and how it works. TL;DR: You typically don't want to sync the entire backend database to every device—whether for capacity or security concerns. Flexible Sync lets the developer provide queries to control exactly what the mobile app asks to sync, together with backend rules to ensure users can only access the data that they're entitled to.
This post builds on that introduction by showing how to add flexible sync to the RChat mobile app. I'll show how to configure the backend Atlas app, and then what code needs adding to the mobile app.
Everything you see in this tutorial can be found in the flex-sync branch of the RChat repo.

Prerequisites

The RChat App

RChat is a messaging app. Users can add other users to a chat room and then share messages, images, and location with each other.
Screen capture video of running the RCha App on 2 different iOS simulators. Two users join a chat room and send messages to each other
All of the user and chat message data is shared between instances of the app via Atlas Device Sync.
There's a common Atlas backend app. There are frontend apps for iOS and Android. This post focuses on the backend and the iOS app.

Configuring the Realm Backend App

The backend app contains a lot of functionality that isn't connected to the sync functionality, and so I won't cover that here. If you're interested, then check out the original RChat series.
As a starting point, you can install the app. I'll then explain the parts connected to Atlas Device Sync.

Import the Backend Atlas App

  1. If you don't already have one, create a MongoDB Atlas Cluster, keeping the default name of Cluster0. The Atlas cluster must be running MongoDB 5.0 or later.
  2. Download the repo and install the Atlas app:
  1. From the Atlas UI, click on the "App Services" tab and you will see the RChat app. Open it and copy the App Id. You'll need to use this before building the iOS app.
Screen capture of how to copy the App ID in the Atlas UI

How Flexible Sync is Enabled in the Back End

Schema

The schema represents how the data will be stored in MongoDB Atlas *and- what the Swift (and Kotlin) model classes must contain.
Each collection/class requires a schema. If you enable the "Developer Mode" option, then Atlas will automatically define the schema based on your Swift or Kotlin model classes. In this case, your imported App includes the schemas, and so developer mode isn't needed. You can view the schemas by browsing to the "Schema" section in the Atlas UI:
Screen capture of schema section of the Atlas UI
You can find more details about the schema/model in Building a Mobile Chat App Using Realm – Data Architecture, but note that for flexible sync (as opposed to the original partition-based sync), the partition field has been removed.
We're interested in the schema for three collections/model-classes:
User:
User documents/objects represent users of the app.
Chatster:
Chatster documents/objects represent a read-only subset of instances of User documents. Chatster is needed because there's a subset of User data that we want to make accessible to all users. E.g., I want everyone to be able to see my username, presence status, and avatar image, but I don't want them to see which chat rooms I'm a member of.
Device Sync lets you control which users can sync which documents. When this article was first published, you couldn't sync just a subset of a document's fields. That's why Chatster was needed. At some point, I can remove Chatster from the app.
ChatMessage:
There's a ChatMessage document object for every message sent to any chat room.

Flexible Sync Configuration

You can view and edit the sync configuration by browsing to the "Sync" section of the Atlas UI:
Enabling Atlas Flexible Device Sync in the Atlas UI
For this deployment, I've selected the Atlas cluster to use. That cluster must be running MongoDB 5.0 or later.
You must specify which fields the mobile app can use in its sync filter queries. Without this, you can't refer to those fields in your sync queries or permissions. You are currently limited to 10 fields.
Scrolling down, you can see the sync permissions:
Screenshot of a JSON document representing sync permissions in the Atlas UI
The UI has flattened the permissions JSON document; here's a version that's easier to read:
The rules component contains a sub-document for each of our collections. Each of those sub-documents contain an array of roles. Each role contains:
  • The name of the role, this should be something that helps other developers understand the purpose of the role (e.g., "admin," "owner," "guest").
  • applyWhen, which defines whether the requesting user matches the role or not. Each of our collections have a single role, and so applyWhen is set to {}, which always evaluates to true.
  • A read rule—how to decide whether this user can view a given document. This is where our three collections impose different rules:
    • A user can read and write to their own User object. No one else can read or write to it.
    • Anyone can read any Chatster document, but no one can write to them. Note that these documents are maintained by database triggers to keep them consistent with their associated User document.
    • The author of a ChatMessage is allowed to write to it. Anyone can read any ChatMessage. Ideally, we'd restrict it to just members of the chat room, but permissions don't currently support arrays—this is another feature that I'm keen to see added.

Adding Flexible Sync to the iOS App

As with the back end, the iOS app is too big to cover in its entirety in this post. I'll explain how to build and run the app and then go through the components relevant to Flexible Sync.

Configure, Build, and Run the RChat iOS App

You've already downloaded the repo containing the iOS app, but you need to change directory before opening and running the app:
Update RChatApp.swift with your App Id (you copied that from the Atlas UI when configuring your backend app). In Xcode, select your device or simulator before building and running the app (⌘R). Select a second device or simulator and run the app a second time (⌘R).
On each device, provide a username and password and select the "Register new user" checkbox: iOS screenshot of registering a new user through the RChat app
Once registered and logged in on both devices, you can create a new chat room, invite your second user, and start sharing messages and photos. To share location, you first need to enable it in the app's settings.

Key Pieces of the iOS App Code

The Model

You've seen the schemas that were defined for the "User," "Chatster," and "ChatMessage" collections in the back end Atlas app. Each of those collections has an associated Realm Object class in the iOS app. Sub-documents map to embedded objects that conform to RealmEmbeddedObject:
UML diagram showing the User, Chatster, and ChatMessage classes—together with their embedded classes
Let's take a close look at each of these classes:
User Class
UML diagram showing the User class—together with its embedded classes
Chatster Class
UML diagram showing the Chatster class—together with its embedded class
ChatMessage Class
UML diagram showing the ChatMessage class—together with its embedded class

Accessing Synced Realm Data

Any iOS app that wants to sync Realm data needs to create a Realm App instance, providing the Realm App ID so that the Realm SDK can connect to the backend Realm app:
When a SwiftUI view (in this case, LoggedInView) needs to access synced data, the parent view must flag that flexible sync will be used. It does this by passing the Realm configuration through the SwiftUI environment:
LoggedInView can then access two variables from the SwiftUI environment:
The users variable is a live query containing all synced User objects in the Realm. But at this point, no User documents have been synced because we haven't subscribed to anything.
That's easy to fix. We create a new function (setSubscription) that's invoked when the view is opened:
Subscriptions are given a name to make them easier to work with. I named this one user_id.
The function checks whether there's already a subscription named user_id. If there is, then the function replaces it. If not, then it adds the new subscription. In either case, the subscription is defined by passing in a query that finds any User documents/objects where the _id field matches the current user's ID.
The subscription should sync exactly one User object to the realm, and so the code for the view's body can work with the first object in the results:
Other views work with different model classes and sync queries. For example, when the user clicks on a chat room, a new view is opened that displays all of the ChatMessages for that conversation:
In this case, the query syncs all ChatMessage objects where the conversationID matches the id of the Conversation object passed to the view.
The view's body can then iterate over all of the matching, synced objects:
As it stands, there's some annoying behavior. If you open conversation A, go back, and then open conversation B, you'll initially see all of the messages from conversation A. The reason is that it takes a short time for the updated subscription to replace the ChatMessage objects in the synced Realm. I solve that by explicitly removing the subscription (which purges the synced objects) when closing the view:
I made a design decision that I'd use the same name ("conversation") for this view, regardless of which conversation/chat room it's working with. An alternative would be to create a unique subscription whenever a new chat room is opened (including the ID of the conversation in the name). I could then avoid removing the subscription when navigating away from a chat room. This second approach would come with two advantages:
  1. The app should be more responsive when navigating between chat rooms (if you'd previously visited the chat room that you're opening).
  2. You can switch between chat rooms even when the device isn't connected to the internet.
The disadvantages of this approach would be:
  1. The app could end up with a lot of subscriptions (and there's a cost to them).
  2. The app continues to store all of the messages from any chat room that you've ever visited from this device. That consumes extra device storage and network bandwidth as messages from all of those rooms continue to be synced to the app.
A third approach would be to stick with a single subscription (named "conversations") that matches every ChatMessage object. The view would then need to apply a filter on the resulting ChatMessage objects so it only displayed those for the open chat room. This has the same advantages as the second approach, but can consume even more storage as the device will contain messages from all chat rooms—including those that the user has never visited.
Note that a different user can log into the app from the same device. You don't want that user to be greeted with someone else's data. To avoid that, the app removes all subscriptions when a user logs out:

Conclusion

In this article, you've seen how to include Flexible Sync in your mobile app. I've shown the code for Swift, but the approach would be the same when building apps with Kotlin, Javascript, or .NET.
Since this post was initially released, Flexible Sync has evolved to include more query and permission operators. For example, array operators (that would allow me to add tighter restrictions on who can ask to read which chat messages).
You can now limit which fields from a document get synced to a given user. This could allow the removal of the Chatster collection, as it's only there to provide a read-only view of a subset of User fields to other users.
Want to suggest an enhancement or up-vote an existing request? The most effective way is through our feedback portal.
Got questions? Ask them in our Community forum.

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

How to migrate from Realm Java SDK to Realm Kotlin SDK


May 09, 2022 | 5 min read
Article

Document our Realm-Powered Swift Frameworks using DocC


May 13, 2022 | 6 min read
Tutorial

Accessing Realm Data on iOS Using Realm Studio


Sep 23, 2022 | 5 min read
Article

Announcing the Realm C++ SDK Alpha


Jun 14, 2023 | 5 min read
Table of Contents