I have a iOS app and I use @AutoOpen to open the realm. I am getting errors such as, “Failed to connect to sync: Host not found (authorative).” I am wondering what is going wrong in my code. It is important for my app to be able to function offline especially for my apps subscription retention.
Here is the code I use @AutoOpen:
import SwiftUI
import RealmSwift
struct OpenSyncedRealmView: View {
@AutoOpen(appId: "Constants.realmAppID", timeout: 2000) var autoOpen
// We need the user so when we add a routine we can assign an ID.
@State var user: User
var body: some View {
switch autoOpen {
case .connecting:
ProgressView()
case .waitingForUser:
ProgressView("Waiting for user to log in...")
case .open(let realm):
MainView(user: user)
.environment(\.realm, realm)
case .progress(let progress):
ProgressView()
case .error(let error):
ErrorView(error: error)
}
}
}
Here is my configurations:
import SwiftUI
import RealmSwift
struct ContentView: View {
@ObservedObject var app: RealmSwift.App
@EnvironmentObject var errorHandler: ErrorHandler
var body: some View {
// Flexible sync uses subscriptions and permissions to determine which data to sync with your App.
if let user = app.currentUser {
let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
// https://github.com/mongodb/docs-realm/blob/5484fdf63344afa70a1142450b1aa8a5ae1feea4/source/examples/generated/swiftui/Authenticate.snippet.flexible-sync-config.swift#L4
let isSetSubscriptionExist = subs.first(named: Constants.mySets)
let isExerciseSubscriptionExist = subs.first(named: Constants.myExercises)
let isRoutineSubscriptionExist = subs.first(named: Constants.myRoutines)
if (isSetSubscriptionExist != nil) && (isExerciseSubscriptionExist != nil) && (isRoutineSubscriptionExist != nil) {
// Existing subscription found - do nothing
return
} else {
subs.append(
QuerySubscription<Routine>(name: Constants.myRoutines) {
$0.owner_id == user.id
})
subs.append(
QuerySubscription<IndividualSet>(name: Constants.mySets) {
$0.owner_id == user.id
})
subs.append(
QuerySubscription<Exercise>(name: Constants.myExercises) {
$0.owner_id == user.id
})
}
})
OpenSyncedRealmView(user: user)
.environment(\.realmConfiguration, config)
}
else {
StartingPageViewTwo()
}
}
}
Thank you for reading my post. Any help would be greatly appreciated.
I have checked and previous package versions this work but not for the latest version. I tried to go back to the previous working package version and got an error.
Should we assume this is a typo “Constants.realmAppID” since that would pass the literal string as a parameter and not the appID which is what is needed
If that was just a typo, then where is the error being thrown - here?
As a guess, something in your sync setup configuration is not right. For example, your AppID could be incorrect or possibly the sync setup. Do all the models have an AppID? Are you authenticated? Is this partition or flex sync?
I believe my AppID is correct everything works completely fine the only problem is when I turn on airplane mode.
I’m not sure what you mean by my models have an AppID? All my objects have an ObjectID parameter.
I am authenticated I use google sign in, apple sign in, and email sign in. All of these work but if I turn on airplane mode I met with sync connection errors after the user has been authenticated.
I am using flexible sync. In my initial post I have the configuration of my flexible synced realm.
I am also not sure it is to do with my configuration as I am able to clone the example repo here setup flexible sync and authentication etc… and get the same issue. In my eyes it seems like the @AutoOpen isn’t working as expected but I could be wrong.
If less I am doing something wrong within my app services online settings configuration?
I have enabled Flexible Sync.
Enabled client recovery.
Enabled max offline time to 30 days.
Have not put anything to the Data Ingest for advanced settings.