Adding the Flexible Sync Configuration
Las thing we need to do is to pass this information to a new view, OpenSyncedRealmView
, that will open the connection with our App and go through all the states needed while logging in our app.
Create OpenSyncedRealmView
Create a new SwiftUI view, add an app
property. We'll need to add that argument to the preview call.
OpenSyncedRealmView
import SwiftUI
import RealmSwift
struct OpenSyncedRealmView: View {
@ObservedObject var app: RealmSwift.App
var body: some View {
Text("Hello, World!")
}
}
struct OpenSyncedRealmView_Previews: PreviewProvider {
static var previews: some View {
OpenSyncedRealmView(app: RealmSwift.App(id: ""))
}
}
Using our config
We'll use our config
in place of the default environment \.realmConfiguration
.
SyncContentView.swift
// Remove this
// Text("Logged in \(user)")
OpenSyncedRealmView(app: app)
.environment(\.realmConfiguration, config)
Final listing for SyncContentView
SyncContentView.swift
import SwiftUI
import RealmSwift
struct SyncContentView: View {
// Observe the Realm app object in order to react to login state changes.
@ObservedObject var app: RealmSwift.App
var body: some View {
if let user = app.currentUser {
// If there is a user logged in
// Create a `flexibleSyncConfiguration` with `initialSubscriptions`.
// We'll inject this configuration as an environment value to use when opening the realm
// in the next view, and the realm will open with these initial subscriptions.
let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
// Check whether the subscription already exists. Adding it more
// than once causes an error.
if let foundSubscriptions = subs.first(named: "user_days") {
// Existing subscription found - do nothing
return
} else {
// Add queries for any objects you want to use in the app
// Linked objects do not automatically get queried, so you
// must explicitly query for all linked objects you want to include
subs.append(QuerySubscription<Days>(name: "user_days", query: {
// Query for objects where the ownerId is equal to the app's current user's id
// This means the app's current user can read and write their own data
$0.ownerId == user.id
}))
subs.append(QuerySubscription<Day>())
subs.append(QuerySubscription<Habits>(name: "user_habits", query: {
// Query for objects where the ownerId is equal to the app's current user's id
// This means the app's current user can read and write their own data
$0.ownerId == user.id
}))
subs.append(QuerySubscription<Habit>())
}
})
OpenSyncedRealmView(app: app)
.environment(\.realmConfiguration, config)
} else {
// If there is no user logged in, show the login view.
ProgressView()
.task{
do {
let myUser = try await app.login(credentials: .anonymous)
} catch {
print("\(error)")
}
}
}
}
}
Log
If you look at the logs you'll see messages like
2023-07-05 17:59:39.708589+0200 GoodHabitsRealmApp[71190:9845203] Sync: Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, client reset = false
2023-07-05 17:59:39.928362+0200 GoodHabitsRealmApp[71190:9845203] Sync: Connected to endpoint 'redacted-ip:443' (from 'my-ip:60529')
2023-07-05 17:59:41.038822+0200 GoodHabitsRealmApp[71190:9845203] Sync: Connection[1]: Connected to app services with request id: "64ah936ca2df64d089058fe9"
2023-07-05 17:59:41.648668+0200 GoodHabitsRealmApp[71190:9845203] Sync: Connection[1]: Session[1]: Begin processing pending FLX bootstrap for query version 0. (changesets: 1, original total changeset size: 0)