Daniel
Daniel

Reputation: 436

Best practice to authenticate synchronized realm

Trying to find any guidance on how to best authenticate to a synchronized realm and making sure not to use any reference to it before.

Let's assume there is no need for a user to login, but e.g. a tableview that is being populated by binding it to a realm.objects query.

If I authenticate to the remote realm in e.g. viewDidLoad() that is too late, applicationDidFinishLaunching() also too late.

I could, of course, show an empty results list first or an empty local realm, but to me that all doesn't look clean.

Any suggestions?

Upvotes: 0

Views: 344

Answers (1)

Dmitry
Dmitry

Reputation: 7340

I'd recommend you to not to use Realm before you have an authenticated user, you can show some login view to handle authentication and show your other view controller after user is authenticated.

// LogInViewController

...

func logIn() {
    SyncUser.authenticate(with: credential, server: serverURL) { user, error in
        if let user = user {
            Realm.Configuration.defaultConfiguration = Realm.Configuration(
                syncConfiguration: (user, syncURL)
            )

            // Show your table view controller or use `try! Realm()`
        } else {
            // Present error
        }
    }
}

Please check also RealmTasks example here: https://github.com/realm/RealmTasks

Upvotes: 0

Related Questions