HomeLearnHow-toUsing Realm Flexible Sync in Your App—an iOS Tutorial

Using Realm Flexible Sync in Your App—an iOS Tutorial

Updated: Feb 24, 2022 |

Published: Feb 14, 2022

  • Realm
  • Swift
  • Swift UI
  • ...

By Andrew Morgan

Rate this article

#Using Realm Flexible Sync in Your App—an iOS Tutorial

#Introduction

We recently announced the release of the Realm Flexible Sync preview—an opportunity for developers to take it for a spin and give us feedback. 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. Realm 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 Realm backend 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 Realm Sync.

There's a common Realm 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 Realm Sync.

#Import the Backend Realm 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 Realm app:
1git clone https://github.com/ClusterDB/RChat.git
2git checkout flex-sync
3cd RChat/RChat-Realm/RChat
4realm-cli login --api-key <your new public key> --private-api-key <your new private key>
5realm-cli import # Then answer prompts, naming the app RChat
  1. From the Atlas UI, click on the Realm logo 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 Realm App ID in the Realm 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 Realm's "Developer Mode" option, then Realm will automatically define the schema based on your Swift or Kotlin model classes. In this case, your imported Realm App includes the schemas, and so developer mode isn't needed. You can view the schemas by browsing to the "Schema" section in the Realm UI:

Screen capture of schema section of the Realm 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:

1{
2 "bsonType": "object",
3 "properties": {
4 "_id": {
5 "bsonType": "string"
6 },
7 "conversations": {
8 "bsonType": "array",
9 "items": {
10 "bsonType": "object",
11 "properties": {
12 "displayName": {
13 "bsonType": "string"
14 },
15 "id": {
16 "bsonType": "string"
17 },
18 "members": {
19 "bsonType": "array",
20 "items": {
21 "bsonType": "object",
22 "properties": {
23 "membershipStatus": {
24 "bsonType": "string"
25 },
26 "userName": {
27 "bsonType": "string"
28 }
29 },
30 "required": [
31 "membershipStatus",
32 "userName"
33 ],
34 "title": "Member"
35 }
36 },
37 "unreadCount": {
38 "bsonType": "long"
39 }
40 },
41 "required": [
42 "unreadCount",
43 "id",
44 "displayName"
45 ],
46 "title": "Conversation"
47 }
48 },
49 "lastSeenAt": {
50 "bsonType": "date"
51 },
52 "presence": {
53 "bsonType": "string"
54 },
55 "userName": {
56 "bsonType": "string"
57 },
58 "userPreferences": {
59 "bsonType": "object",
60 "properties": {
61 "avatarImage": {
62 "bsonType": "object",
63 "properties": {
64 "_id": {
65 "bsonType": "string"
66 },
67 "date": {
68 "bsonType": "date"
69 },
70 "picture": {
71 "bsonType": "binData"
72 },
73 "thumbNail": {
74 "bsonType": "binData"
75 }
76 },
77 "required": [
78 "_id",
79 "date"
80 ],
81 "title": "Photo"
82 },
83 "displayName": {
84 "bsonType": "string"
85 }
86 },
87 "required": [],
88 "title": "UserPreferences"
89 }
90 },
91 "required": [
92 "_id",
93 "userName",
94 "presence"
95 ],
96 "title": "User"
97}

User documents/objects represent users of the app.

Chatster:

1{
2 "bsonType": "object",
3 "properties": {
4 "_id": {
5 "bsonType": "string"
6 },
7 "avatarImage": {
8 "bsonType": "object",
9 "properties": {
10 "_id": {
11 "bsonType": "string"
12 },
13 "date": {
14 "bsonType": "date"
15 },
16 "picture": {
17 "bsonType": "binData"
18 },
19 "thumbNail": {
20 "bsonType": "binData"
21 }
22 },
23 "required": [
24 "_id",
25 "date"
26 ],
27 "title": "Photo"
28 },
29 "displayName": {
30 "bsonType": "string"
31 },
32 "lastSeenAt": {
33 "bsonType": "date"
34 },
35 "presence": {
36 "bsonType": "string"
37 },
38 "userName": {
39 "bsonType": "string"
40 }
41 },
42 "required": [
43 "_id",
44 "presence",
45 "userName"
46 ],
47 "title": "Chatster"
48}

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.

Realm Sync lets you control which users can sync which documents, but it doesn't let you sync just a subset of a document's fields. That's why Chatster is needed. I'm looking forward to when Realm Sync permissions allow me to control access on a per-field (rather than per-document/class) basis. At that point, I can remove Chatster from the app.

ChatMessage:

1{
2 "bsonType": "object",
3 "properties": {
4 "_id": {
5 "bsonType": "string"
6 },
7 "author": {
8 "bsonType": "string"
9 },
10 "authorID": {
11 "bsonType": "string"
12 },
13 "conversationID": {
14 "bsonType": "string"
15 },
16 "image": {
17 "bsonType": "object",
18 "properties": {
19 "_id": {
20 "bsonType": "string"
21 },
22 "date": {
23 "bsonType": "date"
24 },
25 "picture": {
26 "bsonType": "binData"
27 },
28 "thumbNail": {
29 "bsonType": "binData"
30 }
31 },
32 "required": [
33 "_id",
34 "date"
35 ],
36 "title": "Photo"
37 },
38 "location": {
39 "bsonType": "array",
40 "items": {
41 "bsonType": "double"
42 }
43 },
44 "text": {
45 "bsonType": "string"
46 },
47 "timestamp": {
48 "bsonType": "date"
49 }
50 },
51 "required": [
52 "_id",
53 "authorID",
54 "conversationID",
55 "text",
56 "timestamp"
57 ],
58 "title": "ChatMessage"
59}

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 Realm UI:

Enabling Realm Flexible Sync in the Realm UI

For this deployment, I've selected the Atlas cluster to use. That cluster must be running MongoDB 5.0 or later. At the time of writing, MongoDB 5.0 isn't available for shared clusters (including free-tier M0 instances)—that's expected to change very soon, possibly by the time that you're reading this.

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 Realm UI

The UI has flattened the permissions JSON document; here's a version that's easier to read:

1{
2 "rules": {
3 "User": [
4 {
5 "name": "anyone",
6 "applyWhen": {},
7 "read": {
8 "_id": "%%user.id"
9 },
10 "write": {
11 "_id": "%%user.id"
12 }
13 }
14 ],
15 "Chatster": [
16 {
17 "name": "anyone",
18 "applyWhen": {},
19 "read": true,
20 "write": false
21 }
22 ],
23 "ChatMessage": [
24 {
25 "name": "anyone",
26 "applyWhen": {},
27 "read": true,
28 "write": {
29 "authorID": "%%user.id"
30 }
31 }
32 ]
33 },
34 "defaultRoles": [
35 {
36 "name": "all",
37 "applyWhen": {},
38 "read": {},
39 "write": {}
40 }
41 ]
42}

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 Realm 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 Realm 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:

1cd ../../RChat-iOS
2pod install
3open RChat.xcodeproj

Update RChatApp.swift with your Realm App Id (you copied that from the Realm UI when configuring your backend Realm 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 Realm 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

1class User: Object, ObjectKeyIdentifiable {
2 @Persisted(primaryKey: true) var _id = UUID().uuidString
3 @Persisted var userName = ""
4 @Persisted var userPreferences: UserPreferences?
5 @Persisted var lastSeenAt: Date?
6 @Persisted var conversations = List<Conversation>()
7 @Persisted var presence = "On-Line"
8}
9
10class UserPreferences: EmbeddedObject, ObjectKeyIdentifiable {
11 @Persisted var displayName: String?
12 @Persisted var avatarImage: Photo?
13}
14
15class Photo: EmbeddedObject, ObjectKeyIdentifiable {
16 @Persisted var _id = UUID().uuidString
17 @Persisted var thumbNail: Data?
18 @Persisted var picture: Data?
19 @Persisted var date = Date()
20}
21
22class Conversation: EmbeddedObject, ObjectKeyIdentifiable {
23 @Persisted var id = UUID().uuidString
24 @Persisted var displayName = ""
25 @Persisted var unreadCount = 0
26 @Persisted var members = List<Member>()
27}
28
29class Member: EmbeddedObject, ObjectKeyIdentifiable {
30 @Persisted var userName = ""
31 @Persisted var membershipStatus = "User added, but invite pending"
32}

Chatster Class

UML diagram showing the Chatster class—together with its embedded class

1class Chatster: Object, ObjectKeyIdentifiable {
2 @Persisted(primaryKey: true) var _id = UUID().uuidString // This will match the _id of the associated User
3 @Persisted var userName = ""
4 @Persisted var displayName: String?
5 @Persisted var avatarImage: Photo?
6 @Persisted var lastSeenAt: Date?
7 @Persisted var presence = "Off-Line"
8}
9
10class Photo: EmbeddedObject, ObjectKeyIdentifiable {
11 @Persisted var _id = UUID().uuidString
12 @Persisted var thumbNail: Data?
13 @Persisted var picture: Data?
14 @Persisted var date = Date()
15}

ChatMessage Class

UML diagram showing the ChatMessage class—together with its embedded class

1class ChatMessage: Object, ObjectKeyIdentifiable {
2 @Persisted(primaryKey: true) var _id = UUID().uuidString
3 @Persisted var conversationID = ""
4 @Persisted var author: String? // username
5 @Persisted var authorID: String
6 @Persisted var text = ""
7 @Persisted var image: Photo?
8 @Persisted var location = List<Double>()
9 @Persisted var timestamp = Date()
10}
11
12class Photo: EmbeddedObject, ObjectKeyIdentifiable {
13 @Persisted var _id = UUID().uuidString
14 @Persisted var thumbNail: Data?
15 @Persisted var picture: Data?
16 @Persisted var date = Date()
17}

#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:

1let app = RealmSwift.App(id: "rchat-xxxxx") // TODO: Set the Realm application ID

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:

1LoggedInView(userID: $userID)
2 .environment(\.realmConfiguration,
3 app.currentUser!.flexibleSyncConfiguration())

LoggedInView can then access two variables from the SwiftUI environment:

1struct LoggedInView: View {
2 ...
3 @Environment(\.realm) var realm
4 @ObservedResults(User.self) var users

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:

1struct LoggedInView: View {
2 ...
3 @Binding var userID: String?
4 ...
5 var body: some View {
6 ZStack {
7 ...
8 }
9 .onAppear(perform: setSubscription)
10 }
11
12 private func setSubscription() {
13 let subscriptions = realm.subscriptions
14 subscriptions.write {
15 if let currentSubscription = subscriptions.first(named: "user_id") {
16 print("Replacing subscription for user_id")
17 currentSubscription.update(toType: User.self) { user in
18 user._id == userID!
19 }
20 } else {
21 print("Appending subscription for user_id")
22 subscriptions.append(QuerySubscription<User>(name: "user_id") { user in
23 user._id == userID!
24 })
25 }
26 }
27 }
28}

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:

1struct LoggedInView: View {
2 ...
3 @ObservedResults(User.self) var users
4 @Binding var userID: String?
5 ...
6 var body: some View {
7 ZStack {
8 if let user = users.first {
9 ...
10 ConversationListView(user: user)
11 ...
12 }
13 }
14 .navigationBarTitle("Chats", displayMode: .inline)
15 .onAppear(perform: setSubscription)
16 }
17}

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:

1struct ChatRoomBubblesView: View {
2 ...
3 @ObservedResults(ChatMessage.self, sortDescriptor: SortDescriptor(keyPath: "timestamp", ascending: true)) var chats
4 @Environment(\.realm) var realm
5 ...
6 var conversation: Conversation?
7 ...
8 var body: some View {
9 VStack {
10 ...
11 }
12 .onAppear { loadChatRoom() }
13 }
14
15 private func loadChatRoom() {
16 ...
17 setSubscription()
18 ...
19 }
20
21 private func setSubscription() {
22 let subscriptions = realm.subscriptions
23 subscriptions.write {
24 if let conversation = conversation {
25 if let currentSubscription = subscriptions.first(named: "conversation") {
26 currentSubscription.update(toType: ChatMessage.self) { chatMessage in
27 chatMessage.conversationID == conversation.id
28 }
29 } else {
30 subscriptions.append(QuerySubscription<ChatMessage>(name: "conversation") { chatMessage in
31 chatMessage.conversationID == conversation.id
32 })
33 }
34 }
35 }
36 }
37}

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:

1struct ChatRoomBubblesView: View {
2...
3 @ObservedResults(ChatMessage.self,
4 sortDescriptor: SortDescriptor(keyPath: "timestamp", ascending: true)) var chats
5 ...
6 var body: some View {
7 ...
8 ForEach(chats) { chatMessage in
9 ChatBubbleView(chatMessage: chatMessage,
10 authorName: chatMessage.author != user.userName ? chatMessage.author : nil,
11 isPreview: isPreview)
12 }
13 ...
14 }
15}

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:

1struct ChatRoomBubblesView: View {
2 ...
3 @Environment(\.realm) var realm
4 ...
5 var body: some View {
6 VStack {
7 ...
8 }
9 .onDisappear { closeChatRoom() }
10 }
11
12 private func closeChatRoom() {
13 clearSubscription()
14 ...
15 }
16
17 private func clearSubscription() {
18 print("Leaving room, clearing subscription")
19 let subscriptions = realm.subscriptions
20 subscriptions.write {
21 subscriptions.remove(named: "conversation")
22 }
23 }
24}

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:

1struct LogoutButton: View {
2 ...
3 @Environment(\.realm) var realm
4
5
6 var body: some View {
7 Button("Log Out") { isConfirming = true }
8 .confirmationDialog("Are you that you want to logout",
9 isPresented: $isConfirming) {
10 Button("Confirm Logout", role: .destructive, action: logout)
11 Button("Cancel", role: .cancel) {}
12 }
13 .disabled(state.shouldIndicateActivity)
14 }
15
16 private func logout() {
17 ...
18 clearSubscriptions()
19 ...
20 }
21
22 private func clearSubscriptions() {
23 let subscriptions = realm.subscriptions
24 subscriptions.write {
25 subscriptions.removeAll()
26 }
27 }
28}

#Conclusion

In this article, you've seen how to include Realm 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.

This is a preview release and we want your feedback.

Realm Flexible Sync will evolve to include more query and permission operators. Up next, we're looking to expose array operators (that would allow me to add tighter restrictions on who can ask to read which chat messages). We'll also enable querying on embedded documents.

Another feature I'd like to see is to 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.

Rate this article

More from this series

Introducing Realm Flexible Sync Preview
  • Introducing Flexible Sync (Preview) – The Next Iteration of Realm Sync
  • Using Realm Flexible Sync in Your App—an iOS Tutorial
MongoDB logo
© 2021 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.