RealmSwift currentUser cannot be called if more that one valid, logged-in user exists


#1

I have a realm platform and I can register and login a user fine. When I try to open a Synced Realm file with the current User it will not allow it and Xcode throws the error currentUser cannot be called if more that one valid, logged-in user exists. I have followed all the information I can find on realms docs and I can’t make sense as to why this is happening. I have a Login View Controller and a separate View Controller that contains the realm file. Here is example code of the problem I am dealing with.

LogInViewController

import Cocoa
import RealmSwift

class LoginViewController: NSViewController {

@IBOutlet weak var username: NSTextField!
@IBOutlet weak var password: NSSecureTextField!

let realm = try! Realm()

override func viewDidLoad() {
    super.viewDidLoad()
    }

@IBAction func loginButtonPressed(_ sender: NSButton) {

    let login = SyncCredentials.usernamePassword(username: "\(username.stringValue)", password: "\(password.stringValue)", register: false)
            SyncUser.logIn(with: login, server: Constants.AUTH_URL, onCompletion: { [weak self] (user, err) in
                if let _ = user {
                    if let mainWC = self?.view.window?.windowController as? MainWindowController {
        mainWC.segueToHome()
        print("Login Pressed")
    }
    
                } else if let error = err {
                    fatalError(error.localizedDescription)
                }
            })
    
}

SecondViewController

import Cocoa
import RealmSwift

class ViewController: NSViewController {

@IBOutlet weak var textField: NSTextField!

override func viewDidLoad() {
super.viewDidLoad()

    let config = SyncUser.current!.configuration(realmURL: Constants.REALM_URL, fullSynchronization: false, enableSSLValidation: true, urlPrefix: nil)
    self.realm = try! Realm(configuration: config)
}

func save(data: Data) {

        do {

// let realm = try Realm(configuration: config)
try realm.write {
realm.add(data)
}
} catch {
print(“there was an error saving data (error)”)
}
}

@IBAction func saveData(_ sender: NSButton) {
    
            let saveData = Data()
             saveData.textField = textField.stringValue
            save(data: data)
    
}

    func loadData() {

        data = realm.objects(Data.self).sorted(byKeyPath: "timestamp", ascending: false)

    }

}