I am using either the default configuration or sync configuration depending on whether there is a logged in user.
struct ContentView: View {
@ObservedObject var app = Realm.app
var body: some View {
if let config = app.createFlexibleConfiguration() {
MainScreen()
.environment(\.realmConfiguration, config)
.environmentObject(app)
} else {
MainScreen()
.environmentObject(app)
}
}
}
extension Realm {
static let app = RealmSwift.App(id: Config.APP_ID)
}
extension App {
func createFlexibleConfiguration() -> Realm.Configuration? {
if let user = self.currentUser {
let config = user.flexibleSyncConfiguration(initialSubscriptions: { subs in
if subs.first(named: "user_items") == nil {
subs.append(QuerySubscription<Item>(name: "user_items") {
$0.userId == user.id
})
}
})
return config
}
return nil
}
}
It seems to be working as expected already so I am wondering if I even need to use AutoOpen
. (If I login anonymously, a new user is created and no items are displayed. If I log out, I see the items I created before logging in.) Is it “downloading synced changes before opening the realm”, but without listening to the progress (case .progress
)?